Expression ___ has changed after it was checked

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

    As stated by drewmoore, the proper solution in this case is to manually trigger change detection for the current component. This is done using the detectChanges() method of the ChangeDetectorRef object (imported from angular2/core), or its markForCheck() method, which also makes any parent components update. Relevant example:

    import { Component, ChangeDetectorRef, AfterViewInit } from 'angular2/core'
    
    @Component({
      selector: 'my-app',
      template: `
    I'm {{message}}
    `, }) export class App implements AfterViewInit { message: string = 'loading :('; constructor(private cdr: ChangeDetectorRef) {} ngAfterViewInit() { this.message = 'all done loading :)' this.cdr.detectChanges(); } }

    Here are also Plunkers demonstrating the ngOnInit, setTimeout, and enableProdMode approaches just in case.

提交回复
热议问题