Sub topic 13
Migrating an existing application step by step
This chapter assumes you know standalone components, signals (signal, computed), OnPush, and how Zone.js triggers change detection. It gives you an order of work that keeps the app running at each step.
Why migrate in stages
Zoneless mode removes the automatic "run change detection after any async task" behavior. Code that relied on it fails quietly: a field changes, but the view does not. A big-bang switch makes these failures hard to trace. So you first make the app safe for zoneless while Zone.js is still present, and only then remove it.
Step 1: Move components to OnPush
OnPush components are checked only when an input changes, an event fires in the template, a signal read in the template changes, or markForCheck() is called. Those are the same notifications zoneless Angular uses. Setting OnPush everywhere while Zone.js is still running exposes missed updates early, because the app stops getting "free" checks.
Work from leaf components upward. After each batch, click through the screens you changed.
Step 2: Convert state to signals
Find fields that the template reads and that change after async work, such as timers, fetch results, and subscriptions.
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() {
this.count.update((c) => c + 1);
}
}Signals are the preferred fix. When a full conversion is too large for now, call markForCheck() after the change. Use the async pipe for observables, since it calls markForCheck() for you.
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject } from '@angular/core';
@Component({
selector: 'app-legacy-timer',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class LegacyTimer {
private readonly cdr = inject(ChangeDetectorRef);
protected seconds = 0;
constructor() {
setInterval(() => {
this.seconds++;
this.cdr.markForCheck();
}, 1000);
}
}Step 3: Replace NgZone-dependent code
Search the code for NgZone, onStable, onMicrotaskEmpty, isStable, and ngZone.run. In a zoneless app these either do nothing useful or never fire. Replace them with signals, afterNextRender, or PendingTasks. Calls to runOutsideAngular are harmless and can stay until you finish.
If your code starts async work that server rendering or tests must wait for, wrap it in PendingTasks:
import { Injectable, inject, PendingTasks } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataLoader {
private readonly pendingTasks = inject(PendingTasks);
load(url: string) {
return this.pendingTasks.run(async () => {
const response = await fetch(url);
return response.json();
});
}
}Step 4: Enable zoneless mode
Add the provider, keeping zone.js in the build for now. The app is zoneless in behavior, and you can still roll back by removing one line.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { App } from './app/app';
bootstrapApplication(App, {
providers: [provideZonelessChangeDetection()],
});Step 5: Remove zone.js and update tests
Remove "zone.js" from the polyfills lists for build and test in angular.json, and delete any zone.js import in polyfills.ts:
"polyfills": []Tests that used fakeAsync or fixture.detectChanges() to drive updates need to await stability instead:
import { TestBed } from '@angular/core/testing';
import { provideZonelessChangeDetection } from '@angular/core';
import { Counter } from './counter';
describe('Counter', () => {
it('updates the view after a click', async () => {
TestBed.configureTestingModule({
providers: [provideZonelessChangeDetection()],
});
const fixture = TestBed.createComponent(Counter);
await fixture.whenStable();
fixture.nativeElement.querySelector('button').click();
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toContain('Clicked 1 times');
});
});Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| View is stale after a timer or `fetch` | Plain field changed with no notification | Use a signal or call `markForCheck()` |
| Test asserts before the view updates | Missing `await fixture.whenStable()` | Await stability after each action |
| Code waiting on `onStable` hangs | `NgZone` events do not fire | Use `afterNextRender` or `PendingTasks` |
| Library widget does not refresh | Library relies on Zone.js patching | Check its docs and notify Angular manually |
Exercise
Why does the migration order set OnPush on components before enabling zoneless mode, rather than after?
Show answer
OnPush components are checked only on the same notifications zoneless mode uses (signal changes, template events, inputs, markForCheck). Applying it while Zone.js still runs exposes missed updates early, so you can fix them one at a time and then enable zoneless mode with few surprises.