Skip to content

Commit 8d5210c

Browse files
atscottleonsenft
authored andcommitted
feat(core): add ChangeDetectionStrategy.Eager alias for Default
Adds `ChangeDetectionStrategy.Eager` as an explicit alias for `ChangeDetectionStrategy.Default`. This improves readability when contrasting with `OnPush`, clarifying that the component will be checked eagerly when traversal reaches it. Compiler findings: - The compiler resolves `ChangeDetectionStrategy` enum members by value in `resolveEnumValue` (see `packages/compiler-cli/src/ngtsc/annotations/common/src/evaluation.ts`). - Since `Eager` has usage value `1` (same as `Default`), it is correctly interpreted during static analysis. - At runtime, `defineComponent` (in `packages/core/src/render3/definition.ts`) checks `changeDetection === ChangeDetectionStrategy.OnPush` (0). Any other value, including `1` (Eager/Default), results in eager checking behavior (`onPush: false`).
1 parent 496967e commit 8d5210c

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

goldens/public-api/core/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ export interface BootstrapOptions {
209209

210210
// @public
211211
export enum ChangeDetectionStrategy {
212+
// @deprecated
212213
Default = 1,
214+
Eager = 1,
213215
OnPush = 0
214216
}
215217

packages/compiler/src/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export enum ViewEncapsulation {
2929
export enum ChangeDetectionStrategy {
3030
OnPush = 0,
3131
Default = 1,
32+
// tslint:disable-next-line:no-duplicate-enum-values
33+
Eager = 1,
3234
}
3335

3436
export interface Input {

packages/core/src/change_detection/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export enum ChangeDetectionStrategy {
2727
/**
2828
* Use the default `CheckAlways` strategy, in which change detection is automatic until
2929
* explicitly deactivated.
30+
* @deprecated Use `Eager` instead.
3031
*/
3132
Default = 1,
33+
34+
/**
35+
* Use the `Eager` strategy, meaning that the component is checked eagerly when the change
36+
* detection traversal reaches it, rather than only checking under certain circumstances (e.g.
37+
* `markForCheck`, a signal in the template changed, etc).
38+
*/
39+
// tslint:disable-next-line:no-duplicate-enum-values
40+
Eager = 1,
3241
}

packages/core/test/acceptance/change_detection_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,33 @@ describe('change detection', () => {
145145
expect(ref.instance.checks).toBe(2);
146146
});
147147

148+
it('should detect changes for Eager embedded views (alias for Default)', () => {
149+
@Component({
150+
selector: 'eager',
151+
template: '',
152+
changeDetection: ChangeDetectionStrategy.Eager,
153+
})
154+
class EagerComponent {
155+
checks = 0;
156+
ngDoCheck() {
157+
this.checks++;
158+
}
159+
}
160+
161+
@Component({template: '<ng-template #template></ng-template>'})
162+
class Container {
163+
@ViewChild('template', {read: ViewContainerRef, static: true}) vcr!: ViewContainerRef;
164+
}
165+
const fixture = TestBed.createComponent(Container);
166+
const ref = fixture.componentInstance.vcr!.createComponent(EagerComponent);
167+
168+
fixture.detectChanges(false);
169+
expect(ref.instance.checks).toBe(1);
170+
171+
fixture.detectChanges(false);
172+
expect(ref.instance.checks).toBe(2);
173+
});
174+
148175
it('should not detect changes in child embedded views while they are detached', () => {
149176
const counters = {componentView: 0, embeddedView: 0};
150177

0 commit comments

Comments
 (0)