Sub topic 5
How Templates React to Signals: OnPush and Zoneless
This chapter assumes you can build Angular components, know what change detection does in broad terms, and have used signal() and computed() from the earlier chapters. Here you will see what Angular does with a signal once a template reads it.
Templates are signal consumers
A computed() tracks the signals it reads, and a component template does the same. During rendering, Angular records every signal the template calls, such as count(). The template's view becomes a consumer of those signals.
When one of those signals changes, Angular does two things:
- It marks that component's view as needing a refresh.
- It schedules a change detection pass, if one is not already pending.
On the next pass, Angular refreshes the marked view. Ancestors are only traversed so Angular can reach it. Their own bindings are not re-checked.
Signals and OnPush
With ChangeDetectionStrategy.OnPush, Angular checks a component only when it is marked dirty. Before signals, that meant new input references, template events, the async pipe, or a manual markForCheck(). A signal read in the template is now one more trigger, and it is the one you control most directly.
import { ChangeDetectionStrategy, Component, OnDestroy, signal } from '@angular/core';
@Component({
selector: 'app-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Time: {{ now() }}</p>`,
})
export class ClockComponent implements OnDestroy {
now = signal(new Date().toLocaleTimeString());
private timerId = setInterval(() => {
this.now.set(new Date().toLocaleTimeString());
}, 1000);
ngOnDestroy() {
clearInterval(this.timerId);
}
}Nothing calls markForCheck(), yet the clock updates every second. The set() call marks the view itself.
Going zoneless
Zone.js patches browser APIs such as setTimeout, addEventListener and promises so that Angular runs change detection after any async task. Signals tell Angular exactly what changed, so that patching is no longer needed. You enable zoneless mode with one provider:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { ClockComponent } from './app/clock.component';
bootstrapApplication(ClockComponent, {
providers: [provideZonelessChangeDetection()],
});Also remove zone.js from the polyfills array in angular.json. This provider is stable from Angular 20.2, and new projects from Angular 21 are zoneless by default. In older versions the provider was named provideExperimentalZonelessChangeDetection.
Without zones, Angular schedules change detection only when one of these notifications happens:
| Trigger | Example |
|---|---|
| A signal read by a template changes | `count.set(1)` |
| A template event listener fires | `(click)="increment()"` |
| `markForCheck()` is called | Also called by the `async` pipe |
| An input is set through `ComponentRef.setInput()` | Dynamic components |
| A view is attached or removed | `@if` toggling a block |
Common errors
- Mutating plain fields in async callbacks. In zoneless mode,
setTimeout(() => this.label = 'Done')changes the field but never schedules a refresh. Use a signal. - Mutating inside a signal.
items().push(x)keeps the same array reference, so the signal does not notify anyone. Useitems.update(list => [...list, x]). - Copying a signal into a field.
this.total = this.cart.total()inngOnInitreads the value once and loses reactivity. Bindcart.total()in the template, or wrap it incomputed(). - Reading signals only in TypeScript. A signal the template never reads does not mark the view. If the UI depends on a value, the template must call that signal, directly or through a
computed().
Exercise
A zoneless OnPush component has status = 'Loading' and sets this.status = 'Ready' inside a fetch().then() callback. The template shows {{ status }} and never changes. Why, and what is the smallest fix?
Show answer
Without zone.js, nothing tells Angular that the promise resolved, and a plain field assignment is not a change notification. Declare status = signal('Loading'), call this.status.set('Ready') in the callback, and bind {{ status() }} in the template. The signal write marks the view and schedules change detection.
Exercise
In an OnPush component, a template shows {{ count() }} and {{ lastUpdated }}, where lastUpdated is a plain field set in the same method that calls count.set(). Both update on screen. Is this code correct?
Show answer
It works only by accident. The count signal marks the view, and the refresh re-evaluates every binding, including lastUpdated. If count stops changing, or the binding moves to another component, lastUpdated goes stale. Make lastUpdated a signal as well.