Expression ___ has changed after it was checked

后端 未结 17 2050
太阳男子
太阳男子 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:09

    This error is coming because existing value is getting updated immediately after getting initialized. So if you will update new value after existing value is rendered in DOM, Then it will work fine.Like mentioned in this article Angular Debugging "Expression has changed after it was checked"

    for example you can use

    ngOnInit() {
        setTimeout(() => {
          //code for your new value.
        });
    

    }

    or

    ngAfterViewInit() {
      this.paginator.page
          .pipe(
              startWith(null),
              delay(0),
              tap(() => this.dataSource.loadLessons(...))
          ).subscribe();
    }
    

    As you can see i have not mentioned time in setTimeout method. As it is browser provided API, not a JavaScript API, So this will run seperately in browser stack and will wait till call stack items are finished.

    How browser API envokes concept is explained by Philip Roberts in one of Youtube video(What the hack is event loop?).

提交回复
热议问题