Skip to content

Commit ad045ef

Browse files
committed
fix(core): Ensure views marked for check are refreshed during change detection (#54735)
When a view has the `Dirty` flag and is reattached, we should ensure that it is reached and refreshed during the next change detection run from above. In addition, when a view is created and attached, we should ensure that it is reached and refreshed during change detection. This can happen if the view is created and attached outside a change run or when it is created and attached after its insertion view was already checked. In both cases, we should ensure that the view is reached and refreshed during either the current change detection or the next one (if change detection is not already running). We can achieve this by creating all views with the `Dirty` flag set. However, this does happen to be a breaking change in some scenarios. The one identified internally was actually depending on change detection _not_ running immediately because it relied on an input value that was set using `ngModel`. Because `ngModel` sets its value in a `Promise`, it is not available until the _next_ change detection cycle. Ensuring created views run in the current change change detection will result in different behavior in this case. fixes #52928 fixes #15634 BREAKING CHANGE: Newly created and views marked for check and reattached during change detection are now guaranteed to be refreshed in that same change detection cycle. Previously, if they were attached at a location in the view tree that was already checked, they would either throw `ExpressionChangedAfterItHasBeenCheckedError` or not be refreshed until some future round of change detection. In rare circumstances, this correction can cause issues. We identified one instance that relied on the previous behavior by reading a value on initialization which was queued to be updated in a microtask instead of being available in the current change detection round. The component only read this value during initialization and did not read it again after the microtask updated it. PR Close #54735
1 parent ba8e465 commit ad045ef

3 files changed

Lines changed: 5 additions & 10 deletions

File tree

‎packages/core/src/change_detection/flags.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
// TODO(atscott): flip default internally ASAP and externally for v18 (#52928)
10-
let _ensureDirtyViewsAreAlwaysReachable = false;
9+
// TODO(atscott): Remove prior to v18 release. Keeping this around in case anyone internally needs
10+
// to opt out temporarily.
11+
let _ensureDirtyViewsAreAlwaysReachable = true;
1112

1213
export function getEnsureDirtyViewsAreAlwaysReachable() {
1314
return _ensureDirtyViewsAreAlwaysReachable;

‎packages/core/test/acceptance/change_detection_spec.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
import {CommonModule} from '@angular/common';
11-
import {ApplicationRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, Directive, DoCheck, EmbeddedViewRef, ErrorHandler, EventEmitter, inject, Input, NgModule, OnInit, Output, QueryList, TemplateRef, Type, ViewChild, ViewChildren, ViewContainerRef, ɵgetEnsureDirtyViewsAreAlwaysReachable, ɵsetEnsureDirtyViewsAreAlwaysReachable} from '@angular/core';
11+
import {ApplicationRef, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentRef, Directive, DoCheck, EmbeddedViewRef, ErrorHandler, EventEmitter, inject, Input, NgModule, OnInit, Output, QueryList, TemplateRef, Type, ViewChild, ViewChildren, ViewContainerRef} from '@angular/core';
1212
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
1313
import {expect} from '@angular/platform-browser/testing/src/matchers';
1414
import {BehaviorSubject} from 'rxjs';
@@ -184,8 +184,6 @@ describe('change detection', () => {
184184

185185
describe('markForCheck', () => {
186186
it('should mark OnPush ancestor of dynamically created component views as dirty', () => {
187-
const previous = ɵgetEnsureDirtyViewsAreAlwaysReachable();
188-
ɵsetEnsureDirtyViewsAreAlwaysReachable(true);
189187
@Component({
190188
selector: `test-cmpt`,
191189
template: `{{counter}}|<ng-template #vc></ng-template>`,
@@ -245,7 +243,6 @@ describe('change detection', () => {
245243
dynamicCmptRef.changeDetectorRef.reattach();
246244
fixture.detectChanges(false);
247245
expect(fixture.nativeElement).toHaveText('1|dynamic|updatedBinding');
248-
ɵsetEnsureDirtyViewsAreAlwaysReachable(previous);
249246
});
250247

251248
it('should support re-enterant change detection', () => {

‎packages/core/test/linker/change_detection_integration_spec.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ResourceLoader} from '@angular/compiler';
10-
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, DebugElement, Directive, DoCheck, EventEmitter, HostBinding, Injectable, Input, OnChanges, OnDestroy, OnInit, Output, Pipe, PipeTransform, Provider, RendererFactory2, RendererType2, SimpleChange, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, ɵgetEnsureDirtyViewsAreAlwaysReachable, ɵsetEnsureDirtyViewsAreAlwaysReachable} from '@angular/core';
10+
import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, DebugElement, Directive, DoCheck, EventEmitter, HostBinding, Injectable, Input, OnChanges, OnDestroy, OnInit, Output, Pipe, PipeTransform, Provider, RendererFactory2, RendererType2, SimpleChange, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
1111
import {ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing';
1212
import {By} from '@angular/platform-browser/src/dom/debug/by';
1313
import {isTextNode} from '@angular/platform-browser/testing/src/browser_util';
@@ -1136,13 +1136,10 @@ describe(`ChangeDetection`, () => {
11361136
}));
11371137

11381138
it('should allow view to be created in a cd hook', () => {
1139-
const previous = ɵgetEnsureDirtyViewsAreAlwaysReachable();
1140-
ɵsetEnsureDirtyViewsAreAlwaysReachable(true);
11411139
const ctx = createCompFixture('<div *gh9882>{{ a }}</div>', TestData);
11421140
ctx.componentInstance.a = 1;
11431141
ctx.detectChanges();
11441142
expect(ctx.nativeElement.innerText).toEqual('1');
1145-
ɵsetEnsureDirtyViewsAreAlwaysReachable(previous);
11461143
});
11471144

11481145
it('should not throw when two arrays are structurally the same', fakeAsync(() => {

0 commit comments

Comments
 (0)