Sub topic 11
Why ExpressionChangedAfterItHasBeenChecked happens
This chapter assumes you know how a change detection cycle walks the component tree from the root downward, and that you can read Angular templates and lifecycle hooks. You should also be comfortable with @Input or input() and with basic TypeScript.
What the error says
In development mode, Angular runs each change detection pass twice. The first pass updates the DOM. The second pass is a verification pass that re-reads every binding and compares it with the value stored during the first pass. If a value differs, Angular throws ExpressionChangedAfterItHasBeenCheckedError.
The error means that something changed state after Angular had already read it in the same pass. Angular does not go back and re-render, because that could loop forever. It reports the inconsistency instead.
The verification pass does not run in production builds. The error is a development warning that your data flow is not one-directional, so fix the cause instead of ignoring it.
How the flow breaks
Change detection moves top to bottom. A parent's bindings are checked before its children are. If a child changes a value that the parent has already rendered, the parent's binding is out of date by the time the pass ends.
A component that triggers the error
The child below writes to a parent property during its own initialization. The parent has already rendered title by then.
import { Component, inject } from '@angular/core';
@Component({
selector: 'app-child',
template: `<p>Child</p>`,
})
export class Child {
private parent = inject(Parent);
ngOnInit() {
this.parent.title = 'Ready'; // changes a value the parent already rendered
}
}
@Component({
selector: 'app-parent',
imports: [Child],
template: `
<h1>{{ title }}</h1>
<app-child />
`,
})
export class Parent {
title = 'Loading';
}The <h1> binding is checked first and records 'Loading'. The child's ngOnInit then sets 'Ready'. The verification pass sees the new value and throws.
Common causes
- A child or a lifecycle hook such as
ngAfterViewInitwrites to state that an ancestor's template or the same template already displayed. - A template calls a method or getter that returns a new value on each call, for example
Math.random()ornew Date(). - A shared service changes a value while the tree is being checked, and components above the change have already rendered it.
- A binding reads a value that a directive or pipe modifies as a side effect.
Fixes
Choose the fix that restores one-directional data flow.
- Move the state change to where it belongs. Compute the value in the parent, or lift the shared state into a service and update it before the pass starts, for example in response to an event.
- Derive the value instead of writing it. A
computed()signal or a pure template expression always matches its inputs, so nothing needs to be assigned during the pass. - Make template expressions pure. Store a timestamp or random number once in a field and bind to the field.
- Schedule the change for after the current pass when it cannot be avoided. Angular then renders it in a new cycle.
import { ChangeDetectorRef, Component, inject } from '@angular/core';
@Component({
selector: 'app-late',
template: `<p>{{ message }}</p>`,
})
export class Late {
private cdr = inject(ChangeDetectorRef);
message = 'Loading';
ngAfterViewInit() {
Promise.resolve().then(() => {
this.message = 'Ready';
this.cdr.detectChanges();
});
}
}The promise callback runs after the current pass finishes, so the verification pass sees the old value and the new value is rendered by the explicit detectChanges() call.
Exercise
Exercise
A parent template shows {{ now }}, where now is a getter returning new Date().toISOString(). In development the app throws ExpressionChangedAfterItHasBeenCheckedError. Why, and what is a simple fix?
Show answer
The getter returns a different value on each read. The first pass records one timestamp and the verification pass reads a later one, so the values differ. Store the timestamp in a field once, for example in the constructor or an event handler, and bind the template to that field.