Sub topic 10
Signals in templates and OnPush components
This chapter assumes you know how OnPush skips subtrees, how markForCheck works, and the basics of signals (signal, computed, input).
Why signals suit OnPush
An OnPush component is normally checked only when an input reference changes, an event fires in its template, or something marks it dirty. Without signals, you often call markForCheck yourself when state changes from outside those triggers.
Signals remove that step. When a template reads a signal, such as {{ count() }}, Angular records that this component's view depends on the signal. When the signal's value changes, Angular marks that view for refresh. You don't call markForCheck, and the component doesn't need to be Default.
What happens on a signal change
Three details matter:
- Signals compare with
Object.isby default. Writing the same primitive, or the same object reference, notifies nobody. - Ancestors of the dirty view are traversed so Angular can reach it. Their own bindings are not re-evaluated unless they are dirty themselves.
- The signal only schedules a run. Zone.js, or the zoneless scheduler, still decides when the run happens.
A working example
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-cart',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<p>Items: {{ items().length }}</p>
<p>Total: {{ total() }}</p>
<button (click)="add()">Add item</button>
`,
})
export class Cart {
items = signal<number[]>([]);
total = computed(() => this.items().reduce((sum, price) => sum + price, 0));
constructor() {
// A timer does not mark an OnPush view dirty, but a signal write does.
setInterval(() => this.items.update((list) => [...list, 5]), 3000);
}
add() {
this.items.update((list) => [...list, 10]);
}
}The timer in the constructor is the case that needed markForCheck in the ticker example. Here the template reads items() and total(), so each write refreshes the view. computed caches its result, so total() is recalculated only after items changes.
Common errors
Mutating the value inside a signal. This pushes onto the array without writing a new one:
this.items().push(10); // same reference, no notification, no refreshAlways produce a new reference with set or update, as the example does. This is the same immutability rule as for OnPush inputs.
Reading a signal outside a reactive context. Only reads inside the template, a computed, or an effect are tracked. If you copy this.count() into a plain field in ngOnInit, that field never updates.
Mixing plain fields with signals. A plain field changed in a timer is still invisible to OnPush. If the template reads both a signal and a plain field, a signal write refreshes the view and the plain field shows its current value by accident. Remove the dependence on that accident by making the field a signal.
Assuming signals replace Zone.js. In a zone-based app, an async callback that writes a signal triggers a run because the zone fires, and the signal marks the view. In a zoneless app, the signal write itself schedules the run. The next chapters return to this.
Signal inputs
input() and input.required() create signals too. When the parent passes a new reference, the input signal changes and the child's view is marked. Reading user() in the template therefore gives the same behavior as OnPush reference checks, and you can derive values from it with computed.
Exercise
A component uses OnPush and has tags = signal<string[]>([]). A method runs this.tags().push('new') and the template, which reads tags(), does not update. Why, and what is the fix?
Show answer
push mutates the array in place, so the signal's value keeps the same reference and Object.is sees no change. The signal never notifies the view. Replace the array instead: this.tags.update((list) => [...list, 'new']).