Skip to content

fix(children directive): Capture target via closure to fix NgZone compatibility#7241

Merged
janechu merged 2 commits intomicrosoft:mainfrom
Mich0608:fix/observer-target-reference
Feb 27, 2026
Merged

fix(children directive): Capture target via closure to fix NgZone compatibility#7241
janechu merged 2 commits intomicrosoft:mainfrom
Mich0608:fix/observer-target-reference

Conversation

@Mich0608
Copy link
Copy Markdown
Contributor

Pull Request

📖 Description

This PR refactors the ChildrenDirective to use proper closure-based state management instead of dynamically assigning properties to MutationObserver instances, fixing compatibility issues with Angular and improving overall code quality.

Problem with current implementation:
The current code assigns a custom target property directly to MutationObserver instances:

observer.target = target;  // 'target' is not a standard MutationObserver property

This approach has several issues:

  1. Not type-safe: target is not a defined property on MutationObserver, so this relies on JavaScript's dynamic property assignment
  2. Pollutes observer instances: Adding custom properties to browser API objects is an anti-pattern
  3. Breaks with Angular framework patches: When frameworks like Angular's NgZone wrap MutationObserver to intercept callbacks for change detection, the observer instance passed to the callback is the patched version, not the native instance where the target property was assigned. This causes Cannot read property 'target' of undefined errors in Angular applications.

Solution:
Refactor to use closure scope for state management. By creating the callback function inside observe(), the target is naturally captured in the closure, eliminating the need to attach custom properties to the observer.

🎫 Issues

👩‍💻 Reviewer Notes

📑 Test Plan

  • All existing tests for ChildrenDirective should pass unchanged
  • The observable behavior is identical; only the internal implementation changed
  • Manual testing with a patch in PBI Clients and Fabric UX confirms improved compatibility with Angular integrations

✅ Checklist

General

  • I have included a change request file using $ npm run change
  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

⏭ Next Steps

@Mich0608 Mich0608 changed the title Fix/observer target reference fix(children directive): Capture target via closure to fix NgZone compatibility Jan 12, 2026
@janechu janechu force-pushed the fix/observer-target-reference branch from d4ae70f to 90e695e Compare February 17, 2026 19:24
@janechu
Copy link
Copy Markdown
Collaborator

janechu commented Feb 17, 2026

Hi @Mich0608, thanks for the contribution! Could you post a comment to agree to the license?

@Mich0608
Copy link
Copy Markdown
Contributor Author

@janechu Sorry, I just saw this!

@microsoft-github-policy-service agree company="Microsoft"

Michelle Chen added 2 commits February 27, 2026 09:36
@janechu janechu force-pushed the fix/observer-target-reference branch from 90e695e to 25d0a3a Compare February 27, 2026 17:36
@janechu janechu merged commit c992d2a into microsoft:main Feb 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants