← Back to Angular, page 2

angular-0035 · Hard senior

What is zoneless change detection in Angular, how do you enable it, and what must you change in your code?

Zoneless Angular runs change detection only when Angular is told something changed, instead of after every async task patched by zone.js. Enable it with provideZonelessChangeDetection() and remove zone.js. Updates are triggered by signals read in templates, the async pipe, template events, markForCheck, and setInput, so state held in plain fields must move to signals.

Full answer

Zone.js patches browser APIs such as setTimeout, promises, and events. After each patched task, Angular runs change detection over the app. It works without effort, but it adds tens of kilobytes to the bundle, makes stack traces harder to read, and runs checks when nothing visible changed.

Zoneless mode removes zone.js. Angular then needs explicit notifications that a view might be stale. It schedules change detection when:

  • a signal read in a template changes;
  • a template or host event listener runs;
  • the async pipe receives a value;
  • ChangeDetectorRef.markForCheck() is called;
  • ComponentRef.setInput() is called, or a view is attached, or removed.

It does not schedule it when a plain field changes inside a setTimeout, a promise callback, or a third-party library callback. This is the migration cost: the code that "worked by accident" under zone.js stops updating. Fix it by storing the state in a signal.

Enabling: add provideZonelessChangeDetection() to the application config, and remove zone.js from the polyfills array in angular.json (and from the test builder). Status: introduced as experimental in Angular 18, developer preview in 20.0, and stable from 20.2. New CLI projects can be created with a zoneless option.

Preparing an existing app: switch components to OnPush (a component that works with OnPush mostly works zoneless), use signals for state, avoid NgZone.onStable and NgZone.run for logic (they do nothing useful), and test with the zoneless provider in TestBed. A stricter way to find problems is to run with provideZoneChangeDetection() plus the hybrid scheduler, then remove zone.js once everything passes.

Benefits: smaller bundles, faster start-up, fewer checks, and predictable debugging. It also matters for server rendering, where zone.js is awkward.

typescript
// file: main.ts
import { provideZonelessChangeDetection } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { ScoreboardComponent } from './scoreboard.component';

bootstrapApplication(ScoreboardComponent, {
  providers: [provideZonelessChangeDetection()],
}).catch((err) => console.error(err));

// file: scoreboard.component.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';

@Component({
  selector: 'app-scoreboard',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <p>Plain field: {{ plainRuns }}</p>
    <p>Signal: {{ runs() }}</p>
    <button (click)="later()">Update after 1 second</button>
  `,
})
export class ScoreboardComponent {
  plainRuns = 0; // Changed in a timer: the view is NOT refreshed in zoneless mode.
  readonly runs = signal(0); // Read in the template: the view IS refreshed.

  later(): void {
    setTimeout(() => {
      this.plainRuns++;
      this.runs.update((n) => n + 1);
    }, 1000);
  }
}

Angular 20.3. In `angular.json` remove `zone.js` from `polyfills`. The first button in the example does not update the screen in zoneless mode, the second does.

Key points

  • Zoneless removes zone.js and relies on explicit change notifications.
  • Triggers: signals in templates, template events, `async` pipe, `markForCheck`, `setInput`.
  • Plain fields changed in timers or promises no longer update the view. Use signals.
  • Stable from Angular 20.2. Enable with `provideZonelessChangeDetection()` and drop zone.js from polyfills.

Common mistakes

  • Removing zone.js but leaving mutable fields updated in `setTimeout` callbacks, so the screen freezes.
  • Depending on `NgZone.onStable` or `NgZone.run` in application logic.
  • Forgetting to remove zone.js from the test setup, so tests still run in a zone and hide bugs.

Follow-ups

  • How would you migrate a large app step by step?
  • How does a third-party library that relies on zone.js behave?
  • Why does OnPush readiness predict zoneless readiness?

Tips

  • Start with what zone.js does and what it costs. Then explain what replaces it.
  • Give the exact failure example: a plain field set in `setTimeout`.
  • State the status correctly (stable from 20.2). A wrong status is a quick way to lose trust.

Evaluation rubric

  • Explains zone.js role and zoneless triggers.
  • Identifies the plain-field pitfall.
  • Gives a realistic migration plan and correct status.