Sub topic 13
How Change Detection Updates Bindings
This chapter assumes you know interpolation, property binding, event binding and signals from the earlier chapters. You should also know how to create a component with the Angular CLI. Here you will see when Angular rechecks your bindings and why a binding sometimes shows a stale value.
What a Change Detection Pass Does
Every binding in a template is compiled into an update instruction. Angular stores the last value it wrote for each binding. During a change detection pass, Angular evaluates each binding expression again and compares the result to the stored value with ===. If the values differ, Angular writes the new value to the DOM. If they are the same, it skips the write.
Two consequences follow from this:
- Angular can re-evaluate a template expression many times. A method call such as
{{ total() }}on a plain method runs on every pass, so keep template expressions cheap. - Comparison uses reference equality. If you mutate an object or array in place, the reference does not change, and some strategies will not notice the update.
What Triggers a Pass
| Trigger | Default strategy | OnPush strategy |
|---|---|---|
| DOM event handled in the component's template | Checked | Checked |
| Timer or HTTP callback (via zone.js) | Checked | Not checked unless marked dirty |
| New input reference from the parent | Checked | Checked |
| Signal read in the template changes | Checked | Checked |
| `async` pipe emits | Checked | Checked |
With the Default strategy, zone.js patches browser APIs and tells Angular to run a pass after any async task. Angular then checks the whole component tree from the root down. With OnPush, Angular skips a component, and everything below it, unless that component has been marked dirty.
Signals and Change Detection
When a template reads a signal, Angular records that dependency. Calling set() or update() on that signal marks the component for refresh, even under OnPush. This is why signals work well with OnPush and with zoneless apps, which you set up with provideZonelessChangeDetection().
The component below uses OnPush and changes a plain field from a timer. The paragraph never updates, because nothing marks the component dirty.
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-stale-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class StaleClockComponent {
seconds = 0;
constructor() {
setInterval(() => this.seconds++, 1000);
}
}Switching the field to a signal fixes it:
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class ClockComponent {
seconds = signal(0);
constructor() {
setInterval(() => this.seconds.update(s => s + 1), 1000);
}
}Common Errors
- Mutating inputs under OnPush. If the parent calls
items.push(x), the child's[items]input keeps the same reference and the child is not checked. Assign a new array instead:items = [...items, x]. - ExpressionChangedAfterItHasBeenCheckedError. In development mode, Angular runs a second check and throws if a binding's value changed between the two checks. This usually means a lifecycle hook such as
ngAfterViewInitchanges state that is already bound. Move the change earlier, or derive the value withcomputed(). - Expensive template calls. Plain methods in bindings run on every pass. Use a
computed()signal or a pure pipe, which recompute only when their inputs change.
Exercise
A parent component holds todos = ['a'] and passes it to an OnPush child with [todos]="todos". A button in the parent runs this.todos.push('b'). The child's list does not show 'b'. Why, and what is the smallest fix?
Show answer
The push mutates the existing array, so the input reference does not change and Angular does not mark the OnPush child dirty. Replace the array instead: this.todos = [...this.todos, 'b']. You can also store todos in a signal and call todos.update(t => [...t, 'b']).
Exercise
Why does {{ seconds }} stay at 0 in StaleClockComponent, even though zone.js runs a change detection pass after each timer callback?
Show answer
The pass starts, but Angular skips OnPush components that are not marked dirty. A plain field assignment does not mark the component dirty, so its bindings are never re-evaluated.