Sub topic 9
NgZone APIs that no longer do what you expect
This chapter assumes you know standalone components, signals, and how OnPush change detection works. You should also know what Zone.js did in a zone-based app. Here you will see which NgZone APIs still compile in a zoneless app but have stopped doing useful work.
What NgZone was for
NgZone is a service that wraps Zone.js. In a zone-based app, Angular runs your code inside the "Angular zone". When a timer, event handler, or Promise finishes inside that zone, NgZone emits an event, and Angular runs change detection.
With provideZonelessChangeDetection(), Zone.js is not loaded. Angular still provides an NgZone instance, but it is a "noop" version. It does not patch browser APIs and it does not watch for finished tasks.
What each API does now
| API | Zone-based behavior | Zoneless behavior |
|---|---|---|
| `ngZone.run(fn)` | Runs `fn` in the Angular zone, then triggers change detection | Calls `fn` and returns. Nothing is scheduled |
| `ngZone.runOutsideAngular(fn)` | Runs `fn` without triggering change detection | Calls `fn`. There is nothing to opt out of |
| `ngZone.onStable`, `onMicrotaskEmpty`, `onUnstable` | Emit around each change detection turn | Never emit |
| `NgZone.isInAngularZone()` | `true` inside the Angular zone | Always `false` |
| `NgZone.assertInAngularZone()` | Throws outside the zone | Always throws |
The code does not fail loudly. A call to ngZone.run() still runs your function, so the app seems fine until a view fails to update.
import { Component, NgZone, inject } from '@angular/core';
@Component({
selector: 'app-broken',
template: `<p>Ticks: {{ ticks }}</p>`,
})
export class Broken {
private readonly zone = inject(NgZone);
protected ticks = 0;
constructor() {
setInterval(() => {
// Worked in a zone app. Does not schedule a check without zones.
this.zone.run(() => this.ticks++);
}, 1000);
}
}The ticks field changes, but nothing tells Angular. The view stays at 0.
Replacing each pattern
Replace ngZone.run() with a notification Angular understands. The best one is a signal.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-fixed',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Ticks: {{ ticks() }}</p>`,
})
export class Fixed {
protected readonly ticks = signal(0);
constructor() {
setInterval(() => this.ticks.update((t) => t + 1), 1000);
}
}For state you cannot move to a signal yet, call markForCheck() as the previous chapter showed.
Calls to runOutsideAngular() are harmless. Their purpose was to keep high-frequency events such as mousemove from triggering change detection. In a zoneless app only signals, markForCheck(), and a few other notifications schedule a check, so those events already cost nothing. You can leave these calls in place during a migration and remove them later.
Code that subscribes to onStable or onMicrotaskEmpty to learn when rendering finished needs a different tool. Use afterNextRender() or afterEveryRender() for DOM work after a render. Use ApplicationRef.whenStable() or PendingTasks to wait for the app to settle.
import { Component, afterNextRender, ElementRef, inject } from '@angular/core';
@Component({
selector: 'app-measure',
template: `<div>Content</div>`,
})
export class Measure {
private readonly el = inject(ElementRef);
constructor() {
afterNextRender(() => {
console.log(this.el.nativeElement.offsetHeight);
});
}
}Keeping the dependency during migration
Code that mixes both modes can keep NgZone injected. The calls are safe, and you remove them one at a time. Search the code base for NgZone, onStable, onMicrotaskEmpty, and isInAngularZone, then decide whether each use needs a signal, markForCheck(), or a render hook.
Exercise
A component calls this.zone.run(() => this.items.push(item)) inside a WebSocket callback. The list no longer updates after the app goes zoneless. Why, and what is the fix?
Show answer
In a zoneless app ngZone.run() only calls the function and schedules no change detection, and items is a plain array that notifies nothing. Store the list in a signal and update it with this.items.update((list) => [...list, item]). Alternatively, call markForCheck() after the push.
Exercise
Which Angular APIs replace onStable for running code after the DOM has been updated?
Show answer
afterNextRender() for a single run after the next render, and afterEveryRender() for every render. To wait for the whole app to settle, use ApplicationRef.whenStable().