Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression ___ has changed after it was checked

Why is the component in this simple plunk

@Component({   selector: 'my-app',   template: `<div>I'm {{message}} </div>`, }) export class App {   message:string = 'loading :(';    ngAfterViewInit() {     this.updateMessage();   }    updateMessage(){     this.message = 'all done loading :)'   } } 

throwing:

EXCEPTION: Expression 'I'm {{message}} in App@0:5' has changed after it was checked. Previous value: 'I'm loading :( '. Current value: 'I'm all done loading :) ' in [I'm {{message}} in App@0:5]

when all I'm doing is updating a simple binding when my view is initiated?

like image 610
drew moore Avatar asked Dec 18 '15 22:12

drew moore


People also ask

How do you fix expression has changed after it was checked?

Navigate up the call stack until you find a template expression where the value displayed in the error has changed. Ensure that there are no changes to the bindings in the template after change detection is run. This often means refactoring to use the correct component lifecycle hook for your use case.

Has change detection hook been created?

Has it been created in a change detection hook? You've created component dynamically ("outside" of angular lifecycle), this means you need to control change detection manually. Yep!


1 Answers

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: `<div>I'm {{message}} </div>`, }) 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.

like image 82
Kiara Grouwstra Avatar answered Sep 30 '22 15:09

Kiara Grouwstra