Sub topic 2
A refresher on change detection
This chapter reviews how Angular decides when to update the DOM, because zoneless mode changes only the "when" and leaves the rest alone. It assumes you can build standalone components, read basic TypeScript and async code, and have used signal and computed.
What change detection does
Change detection is the process that compares the values your templates read with the values they showed last time, then updates the DOM where they differ. Angular does not watch your fields. It re-reads template bindings during a check and compares the results.
Two questions are separate:
- When does a check run? Something must tell Angular to run one.
- What does a check do? Angular walks the component tree and refreshes bindings.
In a zone-based app, Zone.js answers the first question by reporting that an event handler, timer or Promise finished. Zoneless mode replaces that answer. The second question stays the same.
How a check walks the tree
A check starts at the root and moves down through each component's view. For each view, Angular evaluates the template bindings, compares them with the previous values, and writes changed values to the DOM. The tree is traversed top to bottom, once per check.
A default-strategy example
With the default strategy, every component is checked on every pass.
import { Component } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `
<button (click)="name = 'Angular'">Greet</button>
<p>Hello, {{ name }}</p>
`,
})
export class Greeting {
name = 'world';
}Clicking the button changes name. In a zone-based app, Zone.js sees the click handler finish and triggers a check, so the paragraph updates. Nothing in this component tells Angular that name changed. The check was triggered from outside, and the plain field was read during that check.
Default and OnPush
The default strategy checks a component whenever a pass reaches it. OnPush is stricter. Angular skips an OnPush component unless one of these is true:
- An
@Inputreference changed. - An event listener in its template ran.
- A signal read in its template changed.
markForCheck()was called, or anasyncpipe received a value.
These rules are the base for zoneless mode, which the next chapters cover in detail. A plain field changed by a timer matches none of them.
Common errors
- Mutating an object passed to an OnPush child. The input reference is the same, so the child is skipped. Pass a new object instead.
- ExpressionChangedAfterItHasBeenChecked (NG0100). In development mode, Angular runs a second pass to verify that no binding changed during the first. If a parent binding changes after the parent was checked, for example because a child sets it during its own check, Angular throws. Move the change out of the check, or store the value in a signal.
- Expensive template calls. A method called in a template runs on every check. Prefer
computed()or a plain field.
Exercise
A component uses ChangeDetectionStrategy.OnPush and shows {{ seconds }}, a plain field incremented inside setInterval. The value never updates on screen. Which part of change detection is missing: the walk, the comparison, or the notification?
Show answer
The notification. The check never starts for this component. An OnPush component is checked only after an input change, a template event, a changed signal, or markForCheck(), and a plain field changed in a timer provides none of those. If a check did run, the comparison would find the new value.