Expression ___ has changed after it was checked

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

    You just have to update your message in the right lifecycle hook, in this case is ngAfterContentChecked instead of ngAfterViewInit, because in ngAfterViewInit a check for the variable message has been started but is not yet ended.

    see: https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#afterview

    so the code will be just:

    import { Component } from 'angular2/core'
    
    @Component({
      selector: 'my-app',
      template: `
    I'm {{message}}
    `, }) export class App { message: string = 'loading :('; ngAfterContentChecked() { this.message = 'all done loading :)' } }

    see the working demo on Plunker.

提交回复
热议问题