Sub topic 11
Where OnPush fits in zoneless Angular
This chapter assumes you know how OnPush decides when to check a component, including input reference checks, markForCheck, the async pipe and signals. It also assumes you know what Zone.js does in a default Angular app. Here you will see how those pieces work once Zone.js is removed.
What changes without Zone.js
In a zone-based app, Zone.js patches browser APIs such as setTimeout, promises and DOM events. After any patched task finishes, Angular runs change detection from the root. OnPush then limits how much of the tree that pass visits.
A zoneless app has no such patching. Angular runs change detection only when something tells it to. The notifications are the same ones OnPush already relies on:
- A signal read in a template changes.
markForCheck()is called. The async pipe calls it for you.ComponentRef.setInput()sets an input.- A template or host listener handles an event.
- A view is attached or removed.
So OnPush marks which components need checking, and zoneless decides when a check pass runs at all. Code that already follows OnPush rules usually works with no changes.
Enabling zoneless
Add the zoneless provider when you bootstrap the application.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, {
providers: [provideZonelessChangeDetection()],
});Then remove zone.js from the polyfills entries in your build configuration (and from the test configuration, if it lists it). Without that step the polyfill is still loaded and you ship its weight for nothing. Check your Angular version's documentation for whether new projects already default to zoneless.
Why plain fields stop working
In a zone-based app, a plain field changed inside setTimeout often shows up on screen. The timer ends, Zone.js reports it, and Angular checks the tree. With OnPush you already saw this fail in some cases. Zoneless removes the safety net for Default components too.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-loader',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<p>Plain field: {{ plain }}</p>
<p>Signal: {{ status() }}</p>
`,
})
export class LoaderComponent {
plain = 'loading';
status = signal('loading');
constructor() {
// Nothing notifies Angular, so the view still shows "loading".
setTimeout(() => (this.plain = 'done'), 1000);
// The signal notifies Angular. The view refreshes and now
// shows both "done" values, including the plain field.
setTimeout(() => this.status.set('done'), 2000);
}
}At one second the screen does not change. At two seconds the signal write schedules a pass, and the template re-renders with both new values. The plain field only appeared because something else triggered the refresh. Do not rely on that.
Migrating an existing app
Work in this order:
- Turn on OnPush for components, starting at the leaves and moving up. Fix the stale views you find.
- Replace manually mutated display state with signals, or with Observables shown through the async pipe.
- Keep
markForCheck()only where a callback must write to a plain field, such as a third-party library callback. - Switch on
provideZonelessChangeDetection()and run your tests and the app.
Components that are not OnPush can still run in a zoneless app, but they get no help from Zone.js. They are refreshed only when something notifies Angular, so they need the same discipline.
Testing in a zoneless app
Configure the test bed with the same provider. Prefer await fixture.whenStable() over repeated detectChanges() calls, because it waits for Angular's own scheduled pass instead of forcing one.
import { TestBed } from '@angular/core/testing';
import { provideZonelessChangeDetection } from '@angular/core';
import { SignalCounterComponent } from './signal-counter.component';
describe('SignalCounterComponent (zoneless)', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideZonelessChangeDetection()],
});
});
it('updates after a click', async () => {
const fixture = TestBed.createComponent(SignalCounterComponent);
await fixture.whenStable();
fixture.nativeElement.querySelector('button').click();
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toContain('Count: 1');
});
});If this test hangs or the text never changes, the code under test updates state without notifying Angular. The test is surfacing a real bug.
Summary of the relationship
| Concern | Zone-based | Zoneless |
|---|---|---|
| What starts a pass | Any patched async task | Signals, `markForCheck`, `setInput`, events |
| Role of OnPush | Reduces the work in each pass | Marks components that need work |
| Plain field changed in a timer | May appear by accident | Not shown until something else notifies |
| Recommended state | Anything | Signals and the async pipe |
Exercise
A zoneless OnPush component has total = 0 as a plain field. A fetch callback sets this.total = 42, and the template shows {{ total }}. The screen stays at 0. Give two ways to fix it.
Show answer
Change total to a signal (total = signal(0), set it with this.total.set(42), and read {{ total() }} in the template). Alternatively, inject ChangeDetectorRef and call markForCheck() right after assigning the plain field. Exposing the data as an Observable and using the async pipe also works.