Sub topic 3
Opting in with ChangeDetectionStrategy.OnPush
This chapter assumes you can build components with templates and data bindings, pass data through @Input, and tell an object reference from a copy. You also need to know that Angular's default change detection checks the whole component tree after events. The previous chapter covered that default and its cost. Here you switch one component to a cheaper strategy.
What the strategy changes
Every component has a changeDetection setting. The default is ChangeDetectionStrategy.Default. Angular re-evaluates the template bindings of a Default component on every change detection pass.
ChangeDetectionStrategy.OnPush makes the component conditional. Angular marks it as dirty only when something relevant happens. On a pass, Angular checks a dirty component and skips a clean one. It also skips everything below a clean one.
The trigger list is covered in detail later. For now, remember three triggers:
- An
@Inputreceives a new reference. - An event handler runs in the component's own template or in one of its children.
- The component is explicitly marked for checking.
Enabling OnPush
Add one property to the @Component decorator:
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
export interface User {
name: string;
}
@Component({
selector: 'app-user-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>{{ user.name }}</p>`,
})
export class UserCardComponent {
@Input({ required: true }) user!: User;
}Nothing else in the class or template changes. The component renders the first time it is created, as any component does. After that, Angular rechecks it only when one of the triggers above fires.
How a pass changes
Suppose a button handler in the root component runs. With Default components, Angular walks the entire tree. With OnPush components in the tree, branches with no trigger are skipped.
The diagram shows a point that surprises many developers. Avatar is a Default component, but Angular never reaches it, because the pass stops at its clean OnPush parent. The strategy of a component controls whether its whole subtree is visited, not only whether the component itself is.
Where to apply it first
Start with presentational components. These are components that receive data through inputs and emit events through outputs. They rarely hold state of their own, so the OnPush triggers match how they already work. List items, cards, and table rows are good candidates, especially when they are repeated many times.
Components that mutate shared objects or update fields from callbacks need more care. Convert those after you have read the chapters on immutable updates, the async pipe, and ChangeDetectorRef.
A practical order:
- Add
OnPushto a leaf component. - Run the app and exercise every interaction that touches it.
- Fix any stale views using the techniques in the later chapters.
- Move up the tree.
Common errors when enabling it
| Symptom | Likely cause | Fix |
|---|---|---|
| Child shows old data after the parent edits a property of the input object | The object was mutated, so its reference did not change | Replace the object with a copy (next chapter) |
| Value set in a `setTimeout` or a subscription callback never appears | Nothing marked the component dirty | Use the async pipe, a signal, or `markForCheck` |
| A Default child inside an OnPush parent stops updating | The OnPush parent is clean, so its subtree is skipped | Fix the parent's trigger, not the child |
| Template updates only after you click inside the component | The click is the only trigger that fires | Find what should have marked the component |
Summary of the opt-in
Opting in is a one-line change, but it moves responsibility. With Default, Angular checks everything and you rarely think about it. With OnPush, you commit to delivering changes through channels Angular can observe. In return, Angular skips clean branches and the work per pass shrinks as the tree grows.
Exercise
A parent component renders <app-user-card [user]="user" />. UserCardComponent is OnPush and contains a Default child <app-avatar>. Why does the avatar not update when the parent runs this.user.name = 'Grace', and what triggers would make the avatar check again?
Show answer
The assignment mutates the object, so the user input keeps the same reference. UserCardComponent is not marked dirty and Angular skips it. Because the pass never enters that component, the Default <app-avatar> below it is skipped too. The card would be checked again if the parent passed a new object reference (for example { ...this.user, name: 'Grace' }), if an event fired inside the card or its children, or if the card was explicitly marked for checking.