Sub topic 5
Signals as the primary source of updates
This chapter assumes you can write standalone components, use signal and computed at a basic level, and know what OnPush change detection does. It explains why signals are the most reliable way to update a view in a zoneless app.
Why signals fit zoneless mode
In a zone-based app, Zone.js patches browser APIs such as setTimeout and event listeners. After any of them finishes, Angular checks the whole component tree. Angular never has to know which value changed.
Without Zone.js, Angular needs another way to learn that something changed. A signal provides one. When a template reads a signal, Angular records that the view depends on it. Writing a new value to the signal notifies Angular, which marks that view for refresh and schedules a change detection run.
The update path
The scheduler batches notifications. Several signal writes in the same tick lead to one change detection run, not one per write.
A signal-driven component
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-cart',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<button (click)="addItem()">Add item</button>
<p>{{ count() }} items, total {{ total() }}</p>
`,
})
export class Cart {
private readonly price = 5;
protected readonly count = signal(0);
protected readonly total = computed(() => this.count() * this.price);
addItem() {
this.count.update((c) => c + 1);
}
}The template calls count() and total(), so both are dependencies of the view. addItem() writes to count. That write marks the view for refresh, and total is recalculated when the template reads it again. You never call markForCheck().
The write can happen anywhere, including in a callback Angular doesn't know about. The following code updates the view even though no zone is present:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-clock',
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class Clock {
protected readonly seconds = signal(0);
constructor() {
setInterval(() => this.seconds.update((s) => s + 1), 1000);
}
}Signals carry their own notification, so the timer needs no patching.
What signals do not cover
Only signals read by the template (directly or through computed) notify Angular. A plain field changed in a callback does not.
import { Component } from '@angular/core';
@Component({
selector: 'app-broken',
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class Broken {
protected seconds = 0;
constructor() {
setInterval(() => this.seconds++, 1000);
}
}This component shows 0 forever in a zoneless app. The field is a plain number, so Angular receives no notification. The fix is to convert seconds to a signal. The next chapters cover markForCheck() for code you cannot convert.
Common errors
- Mutating an object or array held in a signal.
list().push(x)changes the data but the signal does not notice. Uselist.update((l) => [...l, x]). - Reading a signal once in the constructor and storing the result. The stored value never updates. Read the signal in the template or in a
computed. - Writing to a signal inside a
computed. Angular forbids this.
Summary of the model
| Situation | Updates the view in zoneless mode? |
|---|---|
| Signal read in the template, then `set` or `update` | Yes |
| `computed` built from signals, read in the template | Yes |
| Plain field changed in `setTimeout` | No |
| Signal mutated in place | No |
Exercise
A component template shows {{ items().length }}. A method runs this.items().push('new') inside a click handler, and the displayed length does not change. Why, and how do you fix it?
Show answer
push mutates the array in place, so the signal's value reference stays the same and the signal does not notify Angular. Replace the array instead: this.items.update((list) => [...list, 'new']).