← Back to Angular, page 7

angular-0135 · Hard senior

How does Angular decide which views to check during change detection, and what do default, OnPush, signals and the dirty flag have to do with it?

Each component has a view (LView) with flags. Default views are checked on every pass. An OnPush view is checked only when it is marked dirty: an input reference changed, an event came from the template, markForCheck was called, an async pipe emitted, or a signal read in its template changed. Signals give each view a reactive consumer, so only views that read a changed signal are refreshed.

Full answer

Angular's template functions run in "update mode" for a view when it is checked. The question is which views are checked on a pass.

Default strategy: every view in the tree is checked on every pass, from the root down. That costs time in large trees, but it is simple and correct.

OnPush strategy: the view is checked only when it is dirty (or on the first pass). A view becomes dirty when:

  1. One of its inputs receives a new value (compared with ===). Mutating an object in place does not change the reference, so nothing happens.
  2. An event handler in its template (or a host listener) runs, for that view and its ancestors.
  3. ChangeDetectorRef.markForCheck() is called, which marks the view and its parents.
  4. The async pipe receives a value (it calls markForCheck).
  5. A signal read in its template changes. Since Angular 17, each view's template acts as a reactive consumer. When a signal that was read during the last check changes, the view is marked for refresh (a lighter flag than dirty, applied to this view and the path to the root so the pass reaches it).

A pass walks from the root, but it skips children of an OnPush view that is not dirty and has no refresh flag. This is why OnPush together with signals is the efficient model: a signal change refreshes one view, and its ancestors are only traversed.

Other details:

  • ngDoCheck of a child runs even when an OnPush child is skipped, because the parent calls it. Use it carefully.
  • Template functions are cheap when nothing changed: each binding compares to the previous value and touches the DOM only for differences.
  • checkNoChanges in development runs a second pass to catch bindings that change during the pass (ExpressionChangedAfterItHasBeenChecked).
  • Zoneless mode removes the "run a pass after every async task" trigger. Passes run when signals, markForCheck or events notify the scheduler.

Interview answer shape: name the two strategies, list the dirty triggers, explain how signals attach to views, and say how this reduces work.

typescript
import { ChangeDetectionStrategy, Component, input, signal } from '@angular/core';

export const renders = { child: 0 };

@Component({
  selector: 'app-score-view',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>{{ tick() }} {{ student().name }}: {{ student().marks }} {{ viaSignal() }}</p>`,
})
export class ScoreViewComponent {
  readonly student = input.required<{ name: string; marks: number }>();
  readonly viaSignal = input.required<number>();

  tick(): string {
    renders.child++; // counts how often this template is evaluated
    return '';
  }
}

@Component({
  selector: 'app-score-page',
  imports: [ScoreViewComponent],
  template: `<app-score-view [student]="student" [viaSignal]="bonus()" />`,
})
export class ScorePageComponent {
  student = { name: 'Asha', marks: 70 };
  readonly bonus = signal(0);

  mutateInPlace(): void {
    this.student.marks = 80; // same reference: OnPush child is not refreshed
  }

  replaceReference(): void {
    this.student = { ...this.student, marks: 90 };
  }
}

Angular 20.3, strict template check passes. A Karma test counts template evaluations: an OnPush child is not re-rendered when the parent mutates an object, but is when a signal it reads changes.

Key points

  • Default views are checked every pass. OnPush views only when dirty or refreshed.
  • Dirty triggers: new input reference, template event, `markForCheck`, async pipe, changed signal read in the template.
  • Signals give each view a reactive consumer, so only affected views refresh.
  • Mutating objects in place does not trigger OnPush. Use new references or signals.

Common mistakes

  • Mutating an input object and expecting an OnPush child to update.
  • Believing OnPush skips `ngDoCheck` of the child.
  • Calling `detectChanges` to compensate for broken data flow.

Follow-ups

  • Why does an event in a child mark its ancestors?
  • How does zoneless mode decide when to run a pass?
  • What does `ExpressionChangedAfterItHasBeenChecked` indicate?

Tips

  • List the five dirty triggers from memory.
  • Say that signals mark views, not components globally.
  • Mention development mode's second pass.

Evaluation rubric

  • Explains default versus OnPush correctly.
  • Lists dirty triggers including signals.
  • Understands the traversal and its cost.