Sub topic 4
OnPush as the foundation for zoneless
This chapter assumes you can build standalone components, use signal and computed, and know how the default change detection model walks the whole component tree. It explains why ChangeDetectionStrategy.OnPush is the model a zoneless app already follows.
What OnPush changes
With the default strategy, Angular checks every component in the tree on each change detection pass. With OnPush, Angular checks a component only when one of these happens:
- An
@Inputreference changes, or a signal input changes. - An event handler declared in the component's template runs, such as
(click). - A signal read in the template changes value.
markForCheck()is called, or theasyncpipe receives a new value.
Otherwise the component is skipped, along with its subtree if nothing inside it is marked dirty. Mutating a plain field, for example from a timer, does not make the component dirty.
Why this matters without Zone.js
In a zone-based app, Zone.js tells Angular after every timer, event, and promise that something may have changed. Angular then runs a pass, but OnPush components still skip themselves unless one of the triggers above applies. Zoneless mode removes that "something may have changed" signal. What remains is the list of triggers that OnPush already uses.
So a component that works correctly under OnPush also works without zones. A default-strategy component that only worked because every pass checked it may stop updating.
A component that is zoneless-ready
This counter uses OnPush, a signal, and a template event. Each of the three triggers it needs is present.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<button (click)="increment()">Clicked {{ count() }} times</button>`,
})
export class Counter {
protected readonly count = signal(0);
increment() {
this.count.update((c) => c + 1);
}
}The click runs a template listener, and count() is a signal read in the template. Either one is enough to schedule a check.
The common failure
The next component uses OnPush but keeps its state in a plain field and changes it from a timer. The timer callback is not a template event and the field is not a signal, so nothing marks the view dirty. The displayed value stays at 0, with or without zones.
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-stuck-timer',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class StuckTimer {
protected seconds = 0;
constructor() {
setInterval(() => {
this.seconds++;
}, 1000);
}
}The fix is to make seconds a signal. A later chapter covers markForCheck() for state you cannot convert.
Default strategy in a zoneless app
Default-strategy components still work in a zoneless app when they are triggered by the same notifications: signals, template events, markForCheck(), and the async pipe. They are not checked just because something unrelated happened. Using OnPush makes this contract explicit and keeps the checks cheap.
Exercise
A component uses OnPush and has the template {{ total }}. A service callback running inside setTimeout does this.total = 5 on a plain field. Will the view update in a zoneless app, and what is the smallest change that makes it update?
Show answer
No. A setTimeout callback is not a template event, and a plain field is not a signal, so nothing marks the component dirty. Declare total as signal(0), read it as {{ total() }}, and set it with this.total.set(5).