Sub topic 5
What makes an OnPush component get checked
This chapter assumes you can build components with inputs and outputs, you know basic RxJS subscriptions, and you know the difference between mutating an object and replacing it. The previous chapters covered how to opt in to ChangeDetectionStrategy.OnPush and why input changes are reference checks. This chapter lists exactly what makes Angular check an OnPush component, so you can predict when a view will update.
The dirty flag model
Under default change detection, Angular checks every component on every pass. An OnPush component is different. Angular skips it unless it is marked dirty. Marking sets a flag on the component's view. When the next pass reaches that view, Angular checks it and clears the flag.
Four things set the flag.
- An input binding receives a new reference.
- An event listener in the component's template (or on its host element) runs.
- Code calls
markForCheck(), directly or through the async pipe. - A signal read in the template changes value.
Anything else that changes a field leaves the view stale. This includes timers, promise callbacks, and subscribe callbacks that assign to plain properties.
Trigger 1: a new input reference
Angular compares each bound input with its previous value using Object.is. A different reference marks the child dirty. Mutating the same object does not. The earlier chapter on immutable updates covers this in detail, so here it is only one entry in the list of triggers.
Trigger 2: events from the template
A listener declared in the template, such as (click)="...", marks the component dirty when it fires. Angular also marks every ancestor up to the root. The ancestors need a pass so that Angular can reach the dirty child.
Listeners added in other ways do not mark anything. Examples are addEventListener on an element you grabbed yourself and fromEvent subscriptions. A @HostListener does count, because Angular registers it as part of the component's host bindings.
Trigger 3: the async pipe and markForCheck
The async pipe calls markForCheck() internally each time its Observable emits. Calling ChangeDetectorRef.markForCheck() yourself does the same job. Both mark the component and all its ancestors dirty. They do not run change detection immediately. They only make sure the next pass reaches the component. The next chapters cover both tools in detail.
Trigger 4: signals
When a template reads a signal, Angular records that dependency. If the signal's value changes, Angular marks that view for refresh. No markForCheck() is needed.
Seeing the difference
This component shows a trigger and a non-trigger side by side.
import {
ChangeDetectionStrategy,
Component,
DestroyRef,
inject,
} from '@angular/core';
@Component({
selector: 'app-triggers',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<p>Clicks: {{ clicks }}</p>
<p>Ticks: {{ ticks }}</p>
<button (click)="clicks = clicks + 1">Click</button>
`,
})
export class TriggersComponent {
clicks = 0;
ticks = 0;
constructor() {
const id = setInterval(() => this.ticks++, 1000);
inject(DestroyRef).onDestroy(() => clearInterval(id));
}
}ticks increases every second, but the page keeps showing Ticks: 0. A timer callback is not a trigger for an OnPush component. Click the button and the template listener marks the view dirty. The check that follows renders both clicks and the current value of ticks. The stale number then jumps forward. The data was never wrong. The view just was not re-rendered.
To fix the display, change ticks into a signal, or call markForCheck() in the interval callback.
What does not mark a component
| Change | Marks the component? |
|---|---|
| Input receives a new object reference | Yes |
| Input object is mutated in place | No |
| `(click)` in the component's own template | Yes |
| `setTimeout` or `setInterval` callback assigns a field | No |
| `subscribe` callback assigns a field | No |
| Async pipe receives a value | Yes |
| Signal read in the template changes | Yes |
Checking is not the same as skipping children
When Angular checks an OnPush component, it checks that component's own bindings. Its children follow their own rules. A default-strategy child inside an OnPush parent is checked whenever the parent is checked. An OnPush child is checked only if its own input reference changed or another trigger applies. When Angular skips an OnPush component, it skips the whole subtree below it, unless a descendant has been marked dirty. A dirty descendant's ancestors are marked too, so the pass can reach it.
Exercise
An OnPush component has a field status = 'loading'. In ngOnInit it subscribes to an HTTP Observable and runs this.status = 'done' in the callback. The template shows {{ status }}, but it still says "loading" after the response arrives. Name the missing trigger and give two ways to fix it.
Show answer
Assigning a plain field in a subscribe callback does not mark the component dirty, so no check happens. Fix it by calling this.cdr.markForCheck() after the assignment. Or expose the result as an Observable and bind it with the async pipe. Storing the status in a signal and reading it in the template also works.