Skip to main content

Zoneless Change Detection

Angular 21 made zoneless change detection the default, removing the dependency on Zone.js. This guide covers what you need to know to run an Ionic Angular app without Zone.js.

With Zone.js, Angular automatically re-renders after almost any asynchronous task. Without it, Angular only re-renders when you explicitly tell it the view is out of date. Most of your app keeps working unchanged, but a few patterns that relied on Zone.js need a small adjustment.

What keeps working automatically​

You do not need to change these. Angular schedules change detection for them in a zoneless app:

  • Template event bindings: (click), (ionChange), (ionInput), and every other (event) handler.
  • Host listeners (@HostListener), including the ones Ionic's form value accessors use, so [(ngModel)] and reactive forms stay in sync.
  • Signal updates that are read in a template.
  • The async pipe.
  • Ionic page lifecycle hooks (ionViewWillEnter, ionViewDidEnter, ionViewWillLeave, ionViewDidLeave) that set state synchronously. Ionic notifies Angular after each hook runs.
  • Navigation, route transitions, and tab switching.
Angular 22

Angular 22 では変更検知のデフォルトが OnPush になります。OnPush では、通常のフィールドに同期的に設定した状態(上記のライフサイクルフック内での設定を含む)は、Ionic が Angular に通知しても自動で再描画されません。signals では引き続きビューが更新されます。アプリシェルへの影響は Angular 22 の変更検知、移行手順は Ionic 9 アップグレードガイドの OnPush Change Detection を参照してください。

What needs a notification​

When you update component state from an asynchronous callback that Angular did not wrap, nothing schedules a re-render. The state changes, but the view does not update. This applies to any Angular code, not only Ionic, and the common sources in an Ionic app are:

  • Awaiting an overlay result, such as await modal.onWillDismiss() or loading.onDidDismiss().then(...).
  • Asynchronous work started inside a lifecycle hook, for example a setTimeout or fetch in ionViewWillEnter.
  • Platform event subscriptions (backButton, resize, pause, resume, keyboard events) and Platform.ready().
  • Any setTimeout, setInterval, or RxJS subscription that assigns to a component field.

You can notify Angular in two ways: write to a signal that the template reads, or inject ChangeDetectorRef and call markForCheck() after the update. We recommend signals because they work the same with or without Zone.js.

Writing a signal that a template reads schedules change detection automatically, so there is nothing extra to remember after the update.

import { Component, inject, signal } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { PickerModal } from './picker.modal';

@Component({
selector: 'app-home',
template: `
<ion-button (click)="pick()">Pick a value</ion-button>
<p>Selected: {{ selected() }}</p>
`,
})
export class HomePage {
private modalCtrl = inject(ModalController);
readonly selected = signal<string | undefined>(undefined);

async pick() {
const modal = await this.modalCtrl.create({ component: PickerModal });
await modal.present();

const { data } = await modal.onWillDismiss<string>();
// Writing the signal updates the view automatically.
this.selected.set(data);
}
}

ChangeDetectorRef.markForCheck()​

If you are not using signals for a particular piece of state, inject ChangeDetectorRef and call markForCheck() after the asynchronous update. It is a no-op-or-better under Zone.js, so it is safe to leave in place if you later re-enable zones.

import { ChangeDetectorRef, Component, inject } from '@angular/core';

@Component({
selector: 'app-list',
template: `@for (item of items; track item) {
<ion-item>{{ item }}</ion-item>
}`,
})
export class ListPage {
private cdr = inject(ChangeDetectorRef);
items: string[] = [];

ionViewWillEnter() {
// Asynchronous work inside a lifecycle hook still needs a notification.
setTimeout(() => {
this.items = ['A', 'B', 'C'];
this.cdr.markForCheck();
}, 1000);
}
}

Common Ionic patterns​

These apply the two approaches above to patterns you are likely to hit in an Ionic app.

Inline overlays with dynamic content​

Content projected into an inline ion-modal or ion-popover follows the same rule. If you populate it asynchronously, update a signal or call markForCheck():

@Component({
selector: 'app-inline',
template: `
<ion-popover #popover>
<ng-template>
<ion-list>
@for (item of items(); track item) {
<ion-item>{{ item }}</ion-item>
}
</ion-list>
</ng-template>
</ion-popover>
`,
})
export class InlinePage {
readonly items = signal<string[]>([]);

open(popover: IonPopover) {
popover.present();
setTimeout(() => this.items.set(['A', 'B', 'C', 'D']), 1000);
}
}

Inline overlays also expose their events as outputs (for example ionModalDidDismiss), which you can convert to a signal with toSignal if you prefer a reactive style.

Platform events​

Platform exposes its events as RxJS subjects. Update a signal inside the subscription so the view reflects the change:

export class AppComponent {
private platform = inject(Platform);
readonly isLandscape = signal(false);

constructor() {
this.platform.resize.subscribe(() => {
this.isLandscape.set(this.platform.isLandscape());
});
}
}

Angular 22 の変更検知​

Angular 22 では戦略を指定しないコンポーネントは OnPush になります。ページの状態を signals ではなく通常のフィールドに保持する場合、アプリのルートから ion-router-outlet または ion-tabs を配置するコンポーネント(アプリシェル)までのすべてのコンポーネントで eager な変更検知が必要です。変更検知はルートから始まり、更新対象になっていない OnPush ビューとその子孫をスキップするため、祖先に OnPush があると、ページ自体が eager でも変更検知が届きません。

import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
selector: 'app-root',
changeDetection: ChangeDetectionStrategy.Eager,
template: '<ion-router-outlet></ion-router-outlet>',
})
export class AppComponent {}

アプリのルートと ion-router-outlet の間に他のコンポーネントがある場合も、それぞれに同じ指定が必要です。状態を signals で更新するページや markForCheck() を呼び出すページでは、どちらも祖先まで更新対象としてマークするため、シェルの指定にかかわらず変更検知が届きます。シェルを eager にする代わりに、ページをこの方式に移行することもできます。

ion-nav 内のページはルートビューとして登録され、ホストコンポーネントとは独立してチェックされるため、どちらの戦略でも動作します。

Staying on Zone.js​

If you are not ready to adopt zoneless change detection, you can opt back into Zone.js with provideZoneChangeDetection(). Refer to the Keeping Zone.js section of the Ionic 9 upgrade guide for the exact configuration.