Why is the component in this simple plunk
@Component({
selector: \'my-app\',
template: `I\'m {{message}} `,
})
export class App {
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.