Amit
Amit

Reputation: 6294

Angular - Expression has changed after it was checked. Previous value

When an expression's value changes, seldom, this error is thrown and the app fails to respond any longer.

I have in my view this function: {{generalService.timeFromNow(item.creation_time)}}

which calls: moment(timestamp, "X").fromNow()

Randomly, when time changed from 35 to 36, this error happened. image

If I have another time moving (ticking) minutes, no error is thrown.

This kind of error happens all around my application, and I do not want to micromanage the ngOnChanges, as I think Angular should manage it.

Upvotes: 1

Views: 285

Answers (1)

n00b
n00b

Reputation: 1852

This is a feature of Angular2 in Development mode to help detection of bad designs. Having this error shows that you probably have to redesign.

For example assume you have two fields with this binding. like a text and a graph.

{{generalService.timeFromNow(item.creation_time)}}

they can end up showing different values in Prod every now and then if you don't address this issue. That would be really difficult to notice in your normal testing and only a small portion of your users will notice it.

The solution is usually to store the result of 'timeFromNow' in some state variable, so that it can not change over the update cycle.

Upvotes: 1

Related Questions