Expression ___ has changed after it was checked

后端 未结 17 1844
太阳男子
太阳男子 2020-11-22 05:53

Why is the component in this simple plunk

@Component({
  selector: \'my-app\',
  template: `
I\'m {{message}}
`, }) export class App {
17条回答
  •  有刺的猬
    2020-11-22 06:13

    The article Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError error explains the behavior in great details.

    The problem with you setup is that ngAfterViewInit lifecycle hook is executed after change detection processed DOM updates. And you're effectively changing the property that is used in the template in this hook which means that DOM needs to be re-rendered:

      ngAfterViewInit() {
        this.message = 'all done loading :)'; // needs to be rendered the DOM
      }
    

    and this will require another change detection cycle and Angular by design only runs one digest cycle.

    You basically have two alternatives how to fix it:

    • update the property asynchronously either using setTimeout, Promise.then or asynchronous observable referenced in the template

    • perform the property update in a hook before the DOM update - ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterContentChecked.

提交回复
热议问题