Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 "Expression has changed after it was checked"

I'm using moment in my app and it shows "X Minutes ago" label this way:

{{comment.created_at | amTimeAgo}}

The number of minutes is updated automically (1 minute later it will be X-1 Miuntes ago".

When I'm away from the app, I'm trying to access it but I get the following error:

platform-browser.umd.js:962 ORIGINAL EXCEPTION: Expression has changed after it was checked. Previous value: '8 Minutes ago'. Current value: '9 Minutes ago'

I read the data (date) once, in a method, from my API.

searchPeople(name)
{
    this.loading = true;
    this.http.get(this.BASE_URL + 'mode=search&name='+name)
      .map((res:Response) => res.json())
      .subscribe(
        data => {       
            this.loading = false;
            this.people = data;
            this.searchedName = name;
        },
        error => {
          console.log(error.text());
        }
      );
}

How I can fix that?

like image 380
TheUnreal Avatar asked Nov 08 '22 13:11

TheUnreal


1 Answers

The problem is caused from the pipe amTimeAgo to return differnt output for the same input. Obviously you compare comment.create_at with Date.now() and that results in different output when time goes by.

That's an impure pipe and you need to tell Angular that by setting pure: false:

@Pipe({ 
  name: 'amTimeAgo',
  pure: false
})
class AmTimeAgoPipe {
  ...
}
like image 136
Günter Zöchbauer Avatar answered Nov 15 '22 11:11

Günter Zöchbauer