ExpressionChangedAfterItHasBeenCheckedError is Angularβs way of telling you that a value changed between when Angular checked it and when it rendered it. This only appears in development mode.
What causes this error
- Modifying data in
ngAfterViewInitβ the view is already checked, then you change something - A parent component changing a childβs input during change detection
- Async operations that complete synchronously β like a resolved Promise in a getter
Fix 1: Use setTimeout or ChangeDetectorRef
// β Causes the error
ngAfterViewInit() {
this.isLoaded = true;
}
// β
Delay to next change detection cycle
ngAfterViewInit() {
setTimeout(() => this.isLoaded = true);
}
// β
Or manually trigger change detection
constructor(private cdr: ChangeDetectorRef) {}
ngAfterViewInit() {
this.isLoaded = true;
this.cdr.detectChanges();
}
Fix 2: Move logic to ngOnInit
If possible, set values in ngOnInit instead of ngAfterViewInit:
// β
ngOnInit runs before the view is checked
ngOnInit() {
this.isLoaded = true;
}
Fix 3: Use async pipe for observables
<!-- β Subscribing in component and setting a property -->
<div>{{ data }}</div>
<!-- β
Use async pipe β Angular handles the timing -->
<div>{{ data$ | async }}</div>
When to ignore it
This error only appears in development mode. Itβs a warning that your data flow might cause issues, but it wonβt crash production. However, fixing it usually means your data flow is cleaner.
Related: React: Too Many Re-renders fix Β· React vs Angular Β· Next.js Hydration Error fix