Sub topic 1
Zoneless Angular Applications: What Zone.js Does for Angular Today
Prerequisites
Assume you can build standalone Angular components, use templates and events, and understand basic signals, Promises, and setTimeout. You should also know that change detection checks component state and updates the DOM when values change.
What Zone.js Does for Angular Today
Zone.js patches asynchronous browser APIs such as timers, events, and Promises. It records when asynchronous work starts and finishes. Angular uses NgZone to observe this activity and schedule change detection.
Zone.js does not compare every value itself. It tells Angular that application work may have completed. Angular then checks the component tree.
A Refresher on Change Detection
Change detection evaluates template bindings against component state. If a binding produces a different value, Angular updates the related DOM node.
In a zone-based application, an event or timer can cause Angular to check the tree even when Angular cannot know which component changed:
| Source of work | Zone-based result |
|---|---|
| `setTimeout` callback | Angular can run change detection afterward |
| Component event | Angular schedules a check |
| Promise continuation | Angular can check after the microtask |
This broad scheduling is convenient, but it can perform unnecessary checks. OnPush reduces the work by checking a component mainly when its inputs change, an event runs in its view, or the component is explicitly marked.
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-message',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>{{ message }}</p>`,
})
export class MessageComponent {
protected message = 'Ready';
}Why Remove Zone.js
Zone.js adds runtime patching and makes scheduling implicit. A small asynchronous operation can trigger checks far beyond the component that needs an update. Implicit scheduling also makes performance and application stability harder to reason about.
Zoneless Angular replaces broad interception with specific notifications. This can reduce work and make update causes clearer. It also exposes code that changed ordinary fields without notifying Angular.
OnPush as the Foundation for Zoneless
OnPush provides a useful mental model: a component is checked when Angular receives a meaningful notification, not after every asynchronous task. Zoneless applications make this model the normal path, so avoid relying on incidental timer or Promise completion.
Signals as the Primary Source of Updates
A signal is reactive state that records which templates read it. When the signal changes, Angular knows which views may need checking.
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(): void {
this.count.update((value) => value + 1);
}
}Exercise
Why can a plain field changed inside a timer fail to update a zoneless view?
Show answer
The field change does not notify Angular. Store the state in a signal or call markForCheck() after changing it.