Sub topic 1
What Zone.js does for Angular today
Prerequisites
This chapter assumes you can build standalone components with templates in modern Angular. It also assumes you know basic TypeScript async code (Promises, setTimeout, DOM events) and the default change detection model. You do not need to know signals in depth yet.
The problem Zone.js solves
Angular must know when to re-render. Your code changes plain fields in response to many things: clicks, timers, HTTP responses, Promise callbacks. Angular does not watch those fields. In a zone-based app, something else tells Angular that "something may have changed."
Zone.js is a library that does this. It patches browser asynchronous APIs, such as addEventListener, setTimeout, Promise and XMLHttpRequest. The patched versions let Zone.js run code before and after each callback.
How NgZone connects Zone.js to Angular
Angular wraps Zone.js in a service called NgZone. When the app starts, Angular runs your code inside the "Angular zone." NgZone exposes an event named onMicrotaskEmpty. It fires when the zone has finished a task and no microtasks (queued Promise callbacks) remain. Angular listens to this event and runs ApplicationRef.tick(), which checks the component tree and updates the DOM.
The key point is that Angular does not ask what changed. It only learns that some asynchronous task finished, then it checks the views.
A concrete example
This component never tells Angular anything. It works in a zone-based app anyway.
import { Component } from '@angular/core';
@Component({
selector: 'app-ticker',
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class Ticker {
protected seconds = 0;
constructor() {
setInterval(() => {
this.seconds++;
}, 1000);
}
}Zone.js has patched setInterval. After each callback runs, NgZone fires, tick() runs, and the template shows the new value. The component code contains no change detection logic at all.
What you get from this
- Automatic updates. Plain mutable fields just work.
- Broad coverage. Third-party code that uses patched APIs also triggers checks.
- Escape hatches.
NgZone.runOutsideAngular()lets you run work, such as high-frequency listeners, without triggering checks.
What it costs
| Cost | Why it happens |
|---|---|
| Extra checks | Any patched task triggers `tick()`, even if no state changed. |
| Bundle size | `zone.js` is shipped as a polyfill. |
| Debugging | Stack traces are longer and wrapped by zone frames. |
| Compatibility | Native `async`/`await` cannot be patched, so the build must downlevel it to Promises. |
| Coverage gaps | APIs Zone.js does not patch, or that run outside the zone, miss updates. |
The last two rows matter most for what follows. Zone.js works by monkey-patching globals, so it depends on the browser APIs it knows about.
What comes next
Zoneless mode replaces the trigger, not the checking. Angular stops listening to NgZone and instead schedules tick() when it gets an explicit notification, mainly from signals. The next chapters cover why this is worth doing and how to enable it.
Exercise
In a zone-based app, a component sets this.count++ inside a setTimeout callback and never calls any Angular API. Why does the view still update? Name the event Angular listens to.
Show answer
Zone.js has patched setTimeout, so it knows when the callback finishes. NgZone then emits onMicrotaskEmpty once no microtasks remain. Angular listens to that event and calls ApplicationRef.tick(), which re-checks the components and updates the DOM.