Header Ads

Expression has changed after it was checked” (NG0100) in your Angular or Ionic app

Seeing “ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked” (NG0100) in your Angular or Ionic app? This guide explains why it only shows up in development and shows 5 fixes with code for lifecycle hooks, getters and child components.

Your Angular or Ionic page works, then you change something small, and the console fills with this error:

ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError:
Expression has changed after it was checked.
Previous value: 'Loading'. Current value: 'Ready'.

Newer versions end the message with a link to the Angular error page for NG0100. The values in your message are the most useful part: they tell you which binding changed, from what and to what.

Why Does This Error Happen?

Angular checks every binding in your template, such as {{ title }} or [disabled]="isLoading", from the top of the component tree to the bottom. In development mode, Angular then runs a second check and compares each binding with the value from the first pass. If a value is different, it throws NG0100.

The error means something changed a bound value after Angular had already read it in the same check cycle. Angular does not want to show a screen that is already out of date, so in development it stops and tells you.

  • In a production build, the second check does not run, so you usually do not see the error. The screen can still be stale or inconsistent, so do not ignore it.
  • The problem is almost always the timing of a change, not the value itself.


Step 1: Read the Message and Find the Binding

  • Previous value and Current value: search your template for the text or property that shows these values.
  • The stack trace: the component name in the trace (for example DemoComponent_Template) shows which template has the changing binding.
  • The hook that caused it: search that component, its children and its services for code that changes this value, and note which lifecycle hook or event it runs in.
Where the value is changedMost likely fix
ngAfterViewInit or ngAfterContentInitSteps 2, 3 or 4
A getter or method used in the templateStep 5
A child component or a service that sets state the parent showsStep 6
A random, date or newly created value in the templateStep 5


Step 2: Move the Change to an Earlier Hook

The classic case is changing a bound property inside ngAfterViewInit, which runs after the view has been checked:

@Component({
selector: 'app-demo',
template: `<p>{{ title }}</p><app-child></app-child>`
})
export class DemoComponent implements AfterViewInit {
title = 'Loading';

@ViewChild(ChildComponent) child!: ChildComponent;

ngAfterViewInit() {
this.title = this.child.name; // changes after the check: NG0100
}
}

If the value does not really depend on the view, set it earlier, in ngOnInit:

ngOnInit() {
this.title = 'Ready'; // runs before the first check of this view
}

If you need a child that is always present, and not inside an *ngIf or *ngFor, a static query makes it available in ngOnInit:

@ViewChild(ChildComponent, { static: true }) child!: ChildComponent;

ngOnInit() {
this.title = this.child.name; // works because the query is static
}

This only works if the value you read is already set when the parent initializes. If the child sets it later, use Step 3 or Step 4 instead.


Step 3: Run Change Detection Again With detectChanges()

When you must read from the view, you can tell Angular to check this component again right after your change:

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

export class DemoComponent implements AfterViewInit {
title = 'Loading';
@ViewChild(ChildComponent) child!: ChildComponent;

constructor(private cdr: ChangeDetectorRef) {}

ngAfterViewInit() {
this.title = this.child.name;
this.cdr.detectChanges(); // check this component again, with the new value
}
}

This is a precise fix, but use it carefully. Calling detectChanges() from a hook that runs on every check can cause loops, so call it once, for a specific change.


Step 4: Delay the Change to the Next Tick

Another common fix is to apply the change after the current check has finished:

ngAfterViewInit() {
Promise.resolve().then(() => {
this.title = this.child.name;
});
}

// or, with a timer:
// setTimeout(() => { this.title = this.child.name; });

With the default Zone.js setup, Angular runs a new check after the delayed code, so the screen updates. This is quick, but it hides the design problem, so use it for cases you understand and not as a reflex. If your app runs without Zone.js, or uses OnPush components, also call markForCheck() or use signals so Angular knows the value changed.


Step 5: Make Template Getters and Expressions Stable

A binding must give the same answer when Angular asks twice in a row. These patterns break that rule:

// returns a different value on every call
get orderId() {
return 'ORD-' + Math.random();
}

// returns a brand new array on every call
get activeItems() {
return this.items.filter((i) => i.active);
}

If you bind either of them, for example [ngForOf]="activeItems" or {{ orderId }}, Angular sees a new value in the second pass and throws NG0100. Create the value once and store it, and update it only when the source data changes:

orderId = 'ORD-' + Math.random();

activeItems: Item[] = [];

ngOnChanges() {
this.activeItems = this.items.filter((i) => i.active);
}

For values that really change over time, such as the current time, update them from a timer or an observable, and bind to the stored property. A pure pipe or a computed signal is another clean way to derive a value without creating it again on every check.


Step 6: Fix Changes Made by Child Components and Services

NG0100 often appears when a child component tells its parent something during initialization, and the parent template shows that value:

// child.component.ts
@Output() ready = new EventEmitter<boolean>();

ngOnInit() {
this.ready.emit(true); // the parent has already checked its template
}

The same happens with a shared service, such as a loading flag that a child component sets while the page template shows a spinner:

ngOnInit() {
this.loadingService.setLoading(true); // parent already read the old value
}

The cleanest fix is to move the state to the parent, or set it earlier in the flow. If that is not possible, publish the change after the current check:

ngOnInit() {
Promise.resolve().then(() => this.ready.emit(true));
Promise.resolve().then(() => this.loadingService.setLoading(true));
}


Ionic Pages: Where It Shows Up

In Ionic, you will most often meet this error in three places: setting a loading flag in ngAfterViewInit after reading @ViewChild(IonContent), a child component toggling a flag that the page shows in an *ngIf, and a getter in the page template that creates a new array or object every time. The Ionic lifecycle events such as ionViewWillEnter run before the view is shown, so they can be a safer place to set page data than ngAfterViewInit, but test your own page, because the exact timing depends on your navigation setup.


What Not to Do

  • Do not wrap every assignment in setTimeout. It treats the symptom, makes the code harder to follow and can cause extra screen flicker.
  • Do not rely on the production build to hide the error. The second check is only a test that exposes the timing bug, and the stale value can still show up for users.
  • Do not catch or ignore the error in a global handler. Fix the cause.


Debug Checklist

  1. Read the previous and current values, and find the binding in your template.
  2. List every place that changes that value, including children, services and observables.
  3. Check which lifecycle hook or event each change runs in.
  4. Move it earlier, run detectChanges() once, or delay it with a microtask.
  5. Replace getters that return new objects or arrays with stored values.


Conclusion

NG0100 is Angular catching a timing mistake: a value changed after the screen was already checked. Start by reading the previous and current values, find the code that changes them, and then pick the smallest fix: change the value earlier, run detectChanges() for one specific update, or publish the change on the next tick. Keep template getters stable, and the error will stop coming back.

Related Guides

Post a Comment

0 Comments