23

What's wrong? Getting the following error in console:

AboutComponent.html:1 ERROR Error: 
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was 
checked. Previous value: 'null: undefined'. Current value: 'null: [object Object]'.
import { AfterViewInit, Component} from '@angular/core';

export class Test {

}

@Component({
  selector: 'app-about',
  template: `
    {{asd}}
  `
})
export class AboutComponent implements AfterViewInit {

  asd: Test;

  ngAfterViewInit(): void {
    this.asd = new Test();
  }
}
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Denis Kildishev
  • 709
  • 2
  • 7
  • 15
  • There is a discussion about this here: https://github.com/angular/angular/issues/14748 – DeborahK Dec 13 '18 at 17:40
  • This is a good reference as well: https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4 – DeborahK Dec 13 '18 at 17:43
  • Follow this... https://stackoverflow.com/questions/34364880/expression-has-changed-after-it-was-checked – Kalanka Jun 20 '21 at 14:51

5 Answers5

34

You can also force change detection after you make a change within ngAfterViewInit() as such:

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

export class Test {

}

@Component({
  selector: 'app-about',
  template: `
    {{asd}}
  `
})
export class AboutComponent implements AfterViewInit {

  asd: Test;

  constructor(
    private cdRef: ChangeDetectorRef   
  ) { }

  ngAfterViewInit(): void {
    this.asd = new Test();
    this.cdRef.detectChanges(); 
  }
}  
Jackson Vaughan
  • 622
  • 6
  • 17
13

Some Lifecycle hooks are called before the rendering part when Angular processes bindings and some are called after that

Ref: https://blog.angularindepth.com/a-gentle-introduction-into-change-detection-in-angular-33f9ffff6f10

The error is due to the Angular call this ngAfterViewInit method once all the binding processed. To get rid this error you can do the following

Move your code inside ngOnInit lifecycle hook

ngOnInit() {
    this.data = new Test()
}

Or

Wrap your code inside setTimeout()

 ngAfterViewInit() {
    setTimeout(()=>this.data = new Test(),0)
  }

Great Article about the same issue: https://blog.angular-university.io/angular-debugging/

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
2

In my case, what helped was addin:

constructor(private ref: ChangeDetectorRef){}

  ngAfterContentChecked() {
    this.ref.detectChanges();
  }
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

I faced this issue while working on Loader subscription with MatSpinner

I fixed it by adding

ngAfterViewInit() {
    this.loading$ = this.loader.loading$;
    this.cdRef.detectChanges();
  }

This piece of code has to be added to the TS file in which you have defined the template., not in any submodule or files below a module if the Template refered is app.component.html where we have a spinnner, the TS file where it should be present is app.component.ts

0

What solved it for me is:

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

Your component must implement: AfterContentChecked and ChangeDetectorRef

Stefana
  • 1
  • 2