← Back to Angular, page 9

angular-0173 · Medium mid

What causes "NG0100: ExpressionChangedAfterItHasBeenCheckedError", why does it appear only in development, and what are the right fixes?

In development mode Angular runs a second check after every pass. If a binding value changed between the two checks, it throws NG0100, because a child or hook changed state after the parent already rendered it. Fix the data flow: compute the value earlier, use signals, or move the update to a later turn such as afterNextRender.

Full answer

Change detection goes from the parent down to the children. After a parent has rendered its bindings, a later step (a child's ngAfterViewInit, a child's setter, a method with side effects) changes something that the parent already displayed. The screen would then show an old value, and the next check would change it again. Angular treats this as a bug in the data flow.

To find such bugs, development mode runs checkNoChanges after each pass: it evaluates all bindings again and compares them with the values in the first pass. If one differs, it throws NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'x'. Current value: 'y'. Production builds skip the second pass, so the error does not appear there, but the screen may show a stale value. So it is a real bug, not only a development warning.

Typical causes:

  1. A child changes a parent's field in ngAfterViewInit or ngAfterContentInit.
  2. A getter or method that returns a different value on each call (Math.random(), new Date(), a counter that increments).
  3. A service that is updated while a template that reads it is being rendered (for example, a loading flag set in a child's constructor).
  4. Content projection or @ViewChild values used in the parent's template before they exist.

Fixes, from best to worst:

  1. Restructure so data flows down: compute the value in the parent (or in a computed) before it is rendered, and pass it down as an input.
  2. Use signals. A signal written in a hook marks the view dirty again, and Angular re-renders, so the second check passes. In many cases, replacing a plain field by a signal is the whole fix.
  3. Move the update after rendering with afterNextRender, or schedule it with queueMicrotask or setTimeout. This is a work-around and delays the update by one turn.
  4. Call cdr.detectChanges() after the change. It works but hides the design problem.

Do not "fix" it by ignoring the error or by disabling the check.

typescript
import { AfterViewInit, Component, Output, EventEmitter, signal } from '@angular/core';

@Component({ selector: 'app-child', template: `child` })
export class ChildComponent implements AfterViewInit {
  @Output() ready = new EventEmitter<string>();
  ngAfterViewInit(): void {
    this.ready.emit('child ready'); // runs after the parent has already rendered its bindings
  }
}

@Component({
  selector: 'app-broken-parent',
  imports: [ChildComponent],
  template: `<p>Status: {{ status }}</p><app-child (ready)="status = $event" />`,
})
export class BrokenParentComponent {
  status = 'waiting'; // plain field: NG0100 in development
}

@Component({
  selector: 'app-fixed-parent',
  imports: [ChildComponent],
  template: `<p>Status: {{ status() }}</p><app-child (ready)="status.set($event)" />`,
})
export class FixedParentComponent {
  readonly status = signal('waiting'); // signal: the view is refreshed again
}

Angular 20.3, strict template check passes. A Karma test renders the broken parent and checks that NG0100 is thrown, then renders the signal-based fix and checks that there is no error and the text is updated.

Key points

  • NG0100 means a binding value changed between the first check and the development-only second check.
  • It happens when a child or hook changes state that a parent already rendered.
  • Production skips the check, but the stale screen bug can still exist.
  • Fix by moving data flow down, using signals, or updating after render.

Common mistakes

  • Treating the error as a development-only nuisance.
  • Using `setTimeout` everywhere without understanding the cause.
  • Returning random or time based values from template methods.

Follow-ups

  • Why does a signal fix the error in many cases?
  • Why does it not appear in a production build?
  • How does `detectChanges()` hide the problem?

Tips

  • Explain the second pass first.
  • Give the `ngAfterViewInit` child to parent case.
  • Order the fixes by quality.

Evaluation rubric

  • Explains the check and the cause.
  • Lists realistic causes.
  • Gives sound fixes in a good order.