Skip to content

fix(core): respect detach() on views attached to ApplicationRef - #71062

Open
edusperoni wants to merge 1 commit into
angular:mainfrom
edusperoni:fix/core-appref-respect-detached-views
Open

edusperoni wants to merge 1 commit into
angular:mainfrom
edusperoni:fix/core-appref-respect-detached-views

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Calling detach() on a view that is attached directly to ApplicationRef has no effect. detach() only clears the Attached flag, 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:

  • Eager component or embedded view: checked on every tick()
  • OnPush component: checked after markForCheck()
  • zoneless: checked when a signal read in the template changes

The same component created through a ViewContainerRef respects detach(), and so does the ChangeDetectorRef injected inside the component (it points to the component view, not the root view).

The only way out today is appRef.detachView(), but the code calling detach() usually doesn't know the view is attached to ApplicationRef, and it also removes the view from the DOM.

Issue Number: #46690

What is the new behavior?

ApplicationRef skips detached views:

  • when refreshing views
  • in the dev mode checkNoChanges pass
  • when deciding if another pass is needed, otherwise a dirty detached view would keep requesting passes until synchronize gives up with NG0103

detectChanges() on a detached view still checks it, and reattach() brings it back to regular change detection.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This matches what detach() documents, but it is a behavior change for anyone calling detach() 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.

`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
@pullapprove
pullapprove Bot requested a review from kirjs September 29, 2026 22:25
@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Sep 29, 2026
@ngbot ngbot Bot added this to the Backlog milestone Sep 29, 2026
@JeanMeche
JeanMeche requested review from atscott and removed request for kirjs September 29, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant