Skip to content

Instantly share code, notes, and snippets.

@vades
Last active April 5, 2024 11:17
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save vades/a225170304f34f88a7a5d48bf4b1f58c to your computer and use it in GitHub Desktop.
Save vades/a225170304f34f88a7a5d48bf4b1f58c to your computer and use it in GitHub Desktop.
Fixing "Expression has changed after it was checked" in Angular

Fixing "Expression has changed after it was checked" in Angular

The exception appears (in the development mode) at the moment the value is checked and value is different of the updated value.

Error message example

AppComponent.html:1 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false'.
    at viewDebugError (core.js:20440)
    at expressionChangedAfterItHasBeenCheckedError (core.js:20428)
    at checkBindingNoChanges (core.js:20530)
    at checkNoChangesNodeInline (core.js:23401)
    ...

Solution

In your component import ChangeDetectorRef, AfterContentChecked

import { Component, OnInit, ChangeDetectorRef, AfterContentChecked } from '@angular/core';

Add ngAfterContentChecked()

 ngAfterContentChecked(): void {
    this.changeDetector.detectChanges();
  }

Component example

src\app\app.component.ts

import { Component, OnInit, ChangeDetectorRef, AfterContentChecked } from '@angular/core';
import { Title } from '@angular/platform-browser';

import { environment } from '@env/environment';
import { LayoutService } from '@app/core/layout/layout.service';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: []
})
export class AppComponent implements OnInit, AfterContentChecked {
  env = environment;
  title = this.env.appName;
  partials: any = {};

  public constructor(
    private titleService: Title,
    public layout: LayoutService,
    private changeDetector: ChangeDetectorRef,
  ) {}

   ngOnInit() {
    this.titleService.setTitle(this.env.appName);
    
    this.layout.getLayout().subscribe(partials => {
      this.partials = partials;
    });
  }

  ngAfterContentChecked(): void {
    this.changeDetector.detectChanges();
  }
}
@mohammedfassinet
Copy link

Thanks. This code worked perfectly for me 👍

@fadingbeat
Copy link

I got this error when unit testing a component. I added the suggested code in the component, and the test is passing. Thank you :) 👍

@Protocol-X
Copy link

This is great, thank you.

@McKenlly
Copy link

McKenlly commented Jul 6, 2022

WTF. GUY, THANK YOU. ♥

@supersuporte
Copy link

Very good. Thanks Sir.

@aostapyshyn
Copy link

OMG. Thank you. Made my day

@yeraldev
Copy link

yeraldev commented Aug 3, 2022

you're really incredible! ty 🙌

@SumitK27
Copy link

I was searching for the solution for a while now but none of them worked.
I wish I would have found this earlier.
Thank you so much.

@DylanPrimera
Copy link

Thanks dude, this works for me!

@yuwankmr
Copy link

It works fine! Thanks a lot.

@Sergi0Martin
Copy link

It works, thanks!

@BruceWeng
Copy link

It's working, thanks!

@kiran-valluri
Copy link

Works good 😊, Thanks

@Israsv0210
Copy link

Very good bro, amazing solution!

@mvnp
Copy link

mvnp commented Jul 12, 2023

Worked for me. Very excellent. Thanks Sir!

@KamaliyaVishal
Copy link

KamaliyaVishal commented Jul 13, 2023

Thank you, @vades , for providing a solution.
I would like to inquire about its potential impact on performance.

By default, the change detection system walks the whole tree. But if you use immutable objects or observables, you can take advantage of them and check parts of the tree only if they “really change”.
These optimizations compose and do not break the guarantees the change detection provides.

Ref : Change Detection in Angular

Copy link

ghost commented Jul 22, 2023

I have used settimeout to resolve this issue

@DoronovIV
Copy link

Has anyone found some better solution? Worked fine for me, but I still doubt the fact that we have to add ChangeDetectorRef to a component that does not work with changeDetection: ChangeDetectionStrategy.OnPush,

@gentunian
Copy link

@DoronovIV I wonder the same, and it looks like it's the appropriate way: https://angular.io/errors/NG0100

check the video section "other options".

@deepjyotiMassoftind
Copy link

Thank you so mush...Its work like magic

@EdwinHauspie
Copy link

EdwinHauspie commented Oct 11, 2023

I'll never understand this ... Angular sees/knows content has changed, but instead of updating the view (again) afterwards, it throws an error. I fail to see the logic in this.

Whereever I see these setTimeout / detectchanges things, it feels like a huge code smell, written by people who don't really know what is happening, including myself ofcourse.

It would be nice if a framework would just handle change detection, like vuejs does.

@luizrr
Copy link

luizrr commented Oct 30, 2023

Nice!

@N-o-m-o-s
Copy link

Thank you

@musicmunky
Copy link

Still saving lives over two years after the original post - thank you so much!!

@kevindqc
Copy link

Doesn't this just gets rid of the error by always running the change detection twice, while doing nothing about the underlying problem?

@spock123
Copy link

Generally this comes if you changes or modify variables that the template is listening to, in your component constructor.

Instead use the AfterViewInit method

@EricNeves
Copy link

Amazing, thanks bro!

@Bagestan
Copy link

Amazing!, thank you!!!

@david-ae
Copy link

Thank you!!!!!

@NarumataSelvaraj
Copy link

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment