Sub topic 13
Zoneless change detection: what changes without Zone.js
This chapter assumes you know how the Default and OnPush strategies work, what markForCheck does, and how signals are read in templates. It explains what Angular does differently when Zone.js is removed.
What Zone.js was doing for you
In the default setup, Zone.js patches browser APIs such as setTimeout, promises and DOM events. After any patched callback finishes, NgZone tells Angular that something may have changed, and Angular runs a change detection cycle from the root.
Without Zone.js, nothing watches those callbacks. Angular needs an explicit signal that a view is stale. That signal is called a notification, and it comes from a small set of sources.
What notifies Angular in zoneless mode
Angular schedules a change detection run when one of these happens:
- A signal read in a template changes.
markForCheckis called, which theasyncpipe also does for you.- A template event listener runs, such as
(click). - A component is created or destroyed, or a view is attached or removed.
Plain property changes inside a timer or a promise do not notify Angular. This is the same limitation OnPush components already have, so OnPush-ready code moves to zoneless mode with little change.
Enabling zoneless mode
Use the zoneless provider when bootstrapping. In recent Angular versions the provider is provideZonelessChangeDetection. Check the name against your installed version, because it was experimental in earlier releases.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { App } from './app/app';
bootstrapApplication(App, {
providers: [provideZonelessChangeDetection()],
});Then remove zone.js from the polyfills array in angular.json. If you leave it there, the library still loads and patches the browser even though Angular ignores it.
Writing a component that works without Zone.js
State that the template reads should live in signals. Updating a signal from a timer is then enough to refresh the view.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class Clock {
seconds = signal(0);
constructor() {
setInterval(() => this.seconds.update((s) => s + 1), 1000);
}
}Compare this with the Ticker component earlier in the tutorial. It used a plain field plus markForCheck. The signal version needs no manual call, because the template read registers the dependency.
Common errors
- A view that never updates. A plain field is changed in a
setTimeoutor a promise callback, and no signal ormarkForCheckis involved. Move the field into a signal or callmarkForCheck. - Code that relies on
NgZone.onStableorNgZone.run. Without Zone.js these events do not fire as before. UseafterNextRenderorafterEveryRenderfor post-render work. - Third-party libraries that assume zones. A library that updates state in its own callbacks may stop refreshing views. Check whether it uses signals or the
asyncpipe. - Leftover
runOutsideAngularcalls. They are harmless, but they no longer have an effect on change detection.
When to choose zoneless
Zoneless mode removes the Zone.js bundle, avoids checks triggered by unrelated events, and gives clearer stack traces. The cost is that every state change must reach Angular through a signal or an explicit call. It fits new applications and codebases that already use OnPush and signals widely. It fits less well where many libraries depend on zone behavior.
Exercise
A zoneless application has a component with a plain field loaded = false. A fetch(...).then(() => { this.loaded = true; }) callback sets it, and the template shows {{ loaded }}. The view stays false. Give two ways to fix it.
Show answer
Change loaded to a signal (loaded = signal(false), set with this.loaded.set(true), read as loaded() in the template), so the template read notifies Angular. Alternatively, inject ChangeDetectorRef and call markForCheck() after setting the field. Converting the value to an observable and using the async pipe also works.
Exercise
Why is an OnPush component with signals considered easy to move to zoneless mode?
Show answer
It already relies on explicit notifications (input changes, events, signals, markForCheck, the async pipe) instead of on Zone.js noticing any async callback, so it does not depend on what Zone.js provided.