Sub topic 3
How Zone.js tells Angular something may have changed
This chapter assumes you know Angular components and templates, TypeScript classes, event and property bindings, and JavaScript async callbacks such as setTimeout and promises.
The problem Zone.js solves
Angular must update the DOM when state changes. But plain properties like this.count++ give Angular no signal that anything happened. Angular needs to know when to run a check.
In the default setup, Zone.js supplies that signal. It does not tell Angular what changed. It tells Angular that something may have changed, and Angular then checks the bindings.
Patching async APIs
Zone.js is a library that patches browser APIs when the app loads. It wraps:
- DOM event listeners (
addEventListener, soclick,input,mousemove) - Timers (
setTimeout,setInterval) - Promises
XMLHttpRequestandfetch
When your code calls one of these, Zone.js remembers which zone was active. When the callback later runs, Zone.js restores that zone around it. A zone is an execution context that survives across async boundaries.
NgZone and the Angular zone
Angular creates a zone for your app and exposes it as NgZone. Code that starts inside it runs in the "Angular zone". NgZone watches for the moment the zone has no more synchronous work queued. It then emits an event, onMicrotaskEmpty. Angular listens for this event and runs a change detection pass from the root.
A worked example
This component has no signals, no markForCheck, and no special code. The view still updates.
import { Component } from '@angular/core';
@Component({
selector: 'app-delayed',
template: `
<p>{{ message }}</p>
<button (click)="start()">Start</button>
`,
})
export class Delayed {
message = 'Waiting';
start() {
setTimeout(() => {
this.message = 'Done';
}, 1000);
}
}The sequence is:
- The click handler runs inside the Angular zone, and a check runs after it. The message is still "Waiting".
setTimeoutis patched, so the timer callback is tied to the Angular zone.- One second later the callback sets
message. When it ends,NgZonereports that it is done. - Angular runs change detection and the template shows "Done".
What Zone.js does not know
Zone.js only knows that a task finished. It cannot tell which property changed or which component owns it. That is why Angular checks the whole tree by default. Later chapters show how OnPush narrows the work.
Zone.js also cannot patch everything. Native async/await cannot be patched directly, so Angular's build tooling downlevels it to promises. Third-party code that schedules work outside the zone will not trigger a check on its own.
Common errors
- Expecting a check after code outside the zone. If you update state in a callback that ran outside the Angular zone, the view stays stale until something else triggers a check.
- Too many checks. Every patched event runs a full pass, including
mousemoveorscrollhandlers that change nothing visible. - Assuming Zone.js detects changes. It detects activity. Angular does the comparing.
Summary of the model
| Piece | Role |
|---|---|
| Zone.js | Patches async APIs and tracks which zone a callback belongs to |
| NgZone | Angular's zone, which reports when its work is done |
| Change detection | Walks the tree and compares bindings after that report |
Exercise
A component updates this.total inside a callback registered with zone.runOutsideAngular(...), and the template shows total. Why does the screen not update, and what is one way to fix it?
Show answer
The callback runs outside the Angular zone, so NgZone never reports that work finished and Angular does not start change detection. Wrap the state update in this.zone.run(() => { this.total = ... }), or otherwise trigger a check, for example with ChangeDetectorRef.markForCheck() combined with a later check, or by updating a signal read in the template.