fix(core): respect detach() on views attached to ApplicationRef - #71062
Open
edusperoni wants to merge 1 commit into
Open
edusperoni wants to merge 1 commit into
edusperoni wants to merge 1 commit into
Conversation
`ChangeDetectorRef.detach()` only clears the `Attached` flag of the view, and that flag is only read when a view is reached through its parent. Views attached directly to `ApplicationRef` are the starting point of the traversal, so they were checked on every tick no matter if they had been detached, both for component host views and for embedded views. `ApplicationRef` now skips detached views when refreshing, when running the dev mode `checkNoChanges` pass and when deciding if another pass is needed. Calling `detectChanges()` on the detached view still checks it, and `reattach()` brings it back to the regular change detection. Fixes angular#46690
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Calling
detach()on a view that is attached directly toApplicationRefhas no effect.detach()only clears theAttachedflag, and that flag is only read when a view is reached through its parent. Root views are where the traversal starts, so they get checked anyway:tick()markForCheck()The same component created through a
ViewContainerRefrespectsdetach(), and so does theChangeDetectorRefinjected inside the component (it points to the component view, not the root view).The only way out today is
appRef.detachView(), but the code callingdetach()usually doesn't know the view is attached toApplicationRef, and it also removes the view from the DOM.Issue Number: #46690
What is the new behavior?
ApplicationRefskips detached views:checkNoChangespasssynchronizegives up with NG0103detectChanges()on a detached view still checks it, andreattach()brings it back to regular change detection.Does this PR introduce a breaking change?
Other information
This matches what
detach()documents, but it is a behavior change for anyone callingdetach()on a root view and relying on it still being checked. Happy to change the target or add a note if you think that's needed.