Sub topic 13
Change detection and OnPush
This chapter assumes you have the TaskService, the TaskItemComponent with input() and output(), and the signal-based state from earlier chapters. You should also know what a template binding is.
What change detection does
Change detection is how Angular decides which parts of the page to update. It re-evaluates the bindings in your templates, such as {{ task().title }} and [checked]="task().done". It writes to the DOM only when a value differs from last time.
By default, Angular checks every component in the tree after any event, such as a click, a timer, or an HTTP response. That is simple and correct. In a large tree it does more work than needed, because most components have not changed.
Switching a component to OnPush
OnPush is a strategy that tells Angular to skip a component unless it has a reason to be checked. Set it in the component decorator:
import { ChangeDetectionStrategy, Component, input, output } from '@angular/core';
import { Task } from './task';
@Component({
selector: 'app-task-item',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<input type="checkbox" [checked]="task().done" (change)="toggled.emit(task().id)" />
<span>{{ task().title }}</span>
<button type="button" (click)="removed.emit(task().id)">Delete</button>
`,
})
export class TaskItemComponent {
readonly task = input.required<Task>();
readonly toggled = output<number>();
readonly removed = output<number>();
}An OnPush component is checked when one of these happens:
- An input receives a new value. Angular compares by reference (
===), not by contents. - An event handler in its own template, or in a child's template, runs.
- A signal that its template reads changes.
- You call
markForCheck()onChangeDetectorRef, or anasyncpipe in the template receives a new value.
<br>
Why immutable updates matter
Your TaskService already replaces the array and the changed task object instead of editing them. With OnPush, that habit becomes a requirement. Compare the two versions of toggle:
// Wrong with OnPush: same object reference, so the child input never "changes".
toggle(id: number): void {
const task = this._tasks().find(t => t.id === id);
if (task) {
task.done = !task.done;
}
}
// Right: a new object for the changed task, so its input reference changes.
toggle(id: number): void {
this._tasks.update(list => list.map(t => (t.id === id ? { ...t, done: !t.done } : t)));
}The first version also never notifies the signal, so computed values like remaining() go stale as well. The second creates a new object for the toggled task. Its TaskItemComponent receives a new reference and is checked. The other items keep their old references and are skipped.
Signals and OnPush work together
When a template reads a signal, Angular records that dependency. If the signal changes, Angular marks that component for checking, even when no input changed. This is why TaskListComponent, which reads tasks() and remaining(), can also use OnPush without extra code:
@Component({
selector: 'app-task-list',
changeDetection: ChangeDetectionStrategy.OnPush,
// template and class unchanged from earlier chapters
})Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Checkbox does not update after a change | An object or array was mutated in place | Replace it with a new object or array |
| A plain class field changes but the view does not | The field is not a signal and no event triggered a check | Store the value in a signal |
| Works under the default strategy, breaks under OnPush | Hidden mutation or a non-signal field | Fix the data flow instead of reverting the strategy |
Exercise
A TaskItemComponent uses OnPush. A parent runs this.tasks()[0].title = 'New title' on the task object it passes in. Why does the item not show the new title, and how do you fix it?
Show answer
The mutation keeps the same object reference, so the input does not change and OnPush skips the component. Replace the task with a copy instead, for example this.tasks.update(list => list.map(t => (t.id === id ? { ...t, title: 'New title' } : t))), so the input receives a new reference.