Sub topic 3
The async pipe and automatic marking
Assume you know Angular components, templates, inputs, and JavaScript callbacks. Angular change detection updates the DOM when a component’s bound state may have changed. Several features tell Angular when that work is needed.
The async pipe and automatic marking
The async pipe subscribes to an Observable or Promise, displays its latest value, and unsubscribes when the component is destroyed. Each emitted value marks the component for checking, including an OnPush component.
import { AsyncPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-status',
imports: [AsyncPipe],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Status: {{ status$ | async }}</p>`,
})
export class StatusComponent {
status$: Observable<string>;
}The observable reference can stay the same. Its emissions still notify Angular because the pipe manages the subscription.
Signals in templates and OnPush components
A signal is a value container that tracks reads and writes. When an OnPush template reads a signal, Angular records that dependency. Updating the signal marks the component for checking.
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 {
count = signal(0);
increment() {
this.count.update((value) => value + 1);
}
}Why ExpressionChangedAfterItHasBeenChecked happens
Angular can report ExpressionChangedAfterItHasBeenCheckedError in development mode when a binding changes after Angular has already checked it during the same cycle. This often happens when ngAfterViewInit or another lifecycle hook changes a value used by the template.
Move initialization earlier, react to an input change, or schedule genuinely deferred work deliberately. Do not hide the error with repeated manual checks.
Running frequent events outside the Angular zone
NgZone.runOutsideAngular prevents frequent callbacks, such as mouse movement, from starting change detection. Re-enter with run only when displayed state changes.
this.zone.runOutsideAngular(() => {
window.addEventListener('mousemove', this.onMove);
});Remove listeners in ngOnDestroy, and call this.zone.run(() => ...) when the UI must update.
Zoneless change detection
Without Zone.js, Angular does not infer changes from every asynchronous callback. It schedules checks from explicit notifications such as signal updates, input changes, event handlers, and markForCheck. This can reduce unnecessary work, but code that mutates state silently must use a supported notification mechanism.
Finding unnecessary checks with Angular DevTools
Use Angular DevTools’ profiler to record an interaction. Inspect the component tree and look for components checked repeatedly without a visible change. Then consider OnPush, signals, the async pipe, or moving noisy listeners outside Angular.
Exercise
An OnPush component displays an observable with async. Must the observable reference change for the view to update?
Show answer
No. Each emission through the async pipe marks the component for checking.