Sub topic 7
What schedules change detection now
This chapter assumes you can build standalone components, use signal and computed, and know what OnPush does. It covers which events make a zoneless app update its view.
The rule: Angular must be told
With Zone.js, any patched async API could trigger a check. These included setTimeout, promises, and DOM events. Zoneless Angular has no such net. A change detection run is scheduled only when something notifies Angular. If nothing does, the view stays as it was.
The built-in scheduler listens for these notifications:
- A signal read in a template changes value.
ChangeDetectorRef.markForCheck()is called. Theasyncpipe calls it for you when it receives a value.- A template or host listener handles an event, such as
(click). - A component is attached to the view tree or removed from it.
- A view is attached with
ViewContainerReforChangeDetectorRef.attach(). - An input is set with
ComponentRef.setInput().
Several triggers may arrive close together. Angular batches them and schedules one run, using setTimeout or requestAnimationFrame, whichever fires first.
A signal drives the update
In this component, the click handler only updates a signal. The template reads count(), so Angular knows the view is dirty.
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) listener is itself a trigger, so this component would refresh even with a plain field. The signal matters when the change happens outside an event, for example in a timer or after a fetch. There the signal write is the only notification Angular gets.
What does not schedule a run
These fail in zoneless mode:
- Changing a plain field inside
setTimeout,setInterval, or a promise callback. - Mutating an object that a template reads, without a signal or
markForCheck(). - Relying on a parent's check to refresh a child that has no signal or event of its own.
The usual symptom is a view that shows old data until the next click.
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject } from '@angular/core';
@Component({
selector: 'app-legacy-timer',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class LegacyTimer {
private readonly cdr = inject(ChangeDetectorRef);
protected seconds = 0;
constructor() {
setInterval(() => {
this.seconds++;
this.cdr.markForCheck();
}, 1000);
}
}Without markForCheck(), seconds would change but the text would not. The call marks the component and its ancestors as needing a check, and that also schedules a run. Making seconds a signal removes the need for the call.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Timer or `fetch` result never appears | Plain field set in an async callback | Use a signal, or call `markForCheck()` |
| Child view is stale | Input object mutated in place | Pass a new reference, or use a signal |
| Works after a click only | The click is the only trigger | Notify Angular where the data changes |
Exercise
A component sets this.items = await response.json() in a method called from ngOnInit. The template reads items as a plain field, and the app is zoneless. The list never shows. Give two fixes.
Show answer
Make items a signal and write to it with items.set(...), then read items() in the template. Alternatively, inject ChangeDetectorRef and call markForCheck() after the assignment. The signal is preferred because the template then declares its own dependency.