Sub topic 8
Handling non-signal state with markForCheck and the async pipe
This chapter assumes you can write standalone components, use signal and computed, and know how OnPush change detection works. You also need to know that a zoneless app only checks a view when something tells Angular to.
Why plain fields stop updating
In a zone-based app, Zone.js patches setTimeout, promises and DOM events. After any of them runs, Angular checks the whole tree. A plain field such as seconds = 0 therefore appears to update on its own.
Without Zone.js, nothing watches those callbacks. Changing a plain field does not tell Angular anything, so the view stays stale. Signals read in a template, template event listeners, the async pipe and markForCheck() do send that notification.
Option 1: move the state into a signal
A signal is the preferred fix. Reading it in the template registers the dependency, and writing it marks the component dirty. If you control the code, convert the field.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-signal-timer',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class SignalTimer {
protected readonly seconds = signal(0);
constructor() {
setInterval(() => this.seconds.update((s) => s + 1), 1000);
}
}Option 2: call markForCheck()
Sometimes you cannot convert the state. It may be a plain field, a value written by a library, or a field in a large class that you are migrating step by step. In that case, call markForCheck() on the component's ChangeDetectorRef after you change the field.
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);
}
}markForCheck() marks this component and its ancestors as needing a check, and it schedules a change detection run. It does not run change detection immediately. Several calls in the same tick lead to one update.
Option 3: use the async pipe
For Observables and Promises, the async pipe subscribes in the template. When a new value arrives, it calls markForCheck() for you. It also unsubscribes when the component is destroyed.
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { interval, map } from 'rxjs';
@Component({
selector: 'app-rx-timer',
imports: [AsyncPipe],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds$ | async }}</p>`,
})
export class RxTimer {
protected readonly seconds$ = interval(1000).pipe(map((n) => n + 1));
}If you subscribe manually in the class and assign the result to a field, the view will not update. Either use async, or convert the stream with toSignal() from @angular/core/rxjs-interop.
Choosing between them
| Situation | Use |
|---|---|
| New state owned by the component | `signal()` |
| Observable consumed by the template | `async` pipe or `toSignal()` |
| Plain field you cannot convert yet | `markForCheck()` |
| Value changed in a callback from a library | Write a signal, or call `markForCheck()` in the callback |
Common errors
- Changing a field in a
setTimeout,fetchorsubscribecallback and forgettingmarkForCheck(). - Calling
markForCheck()before the field changes. The check is scheduled and then runs later, so the order is usually harmless. Still, call it after the change to keep the intent clear. - Subscribing to an Observable in
ngOnInitand storing the value in a plain field.
Exercise
A component subscribes to this.userService.user$ in its constructor and stores each value in a plain field user. The template shows {{ user?.name }}. The app is zoneless and the name never appears. Give two ways to fix it.
Show answer
Option 1: remove the manual subscription and use user$ | async in the template, or convert the stream with toSignal(this.userService.user$) and read the signal in the template. Option 2: keep the subscription and call this.cdr.markForCheck() after assigning user. The first option is preferred because Angular tracks the update and handles unsubscription for you.