Explaining the Angular Error: 'Expression Changed After It Has Been Checked'


Welcome to “Continuous Improvement,” the podcast where we explore tips, tricks, and valuable insights to help you enhance your development skills. I’m your host, Victor, and in today’s episode, we’ll be discussing a common error message that many developers encounter while working with Angular.

So, picture this: one of our colleagues is deep into developing an Angular frontend application when suddenly, an error message pops up on their screen. The message reads: “ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.”

Now, this error occurred while our colleague was working on implementing a back button feature. Specifically, it happened when navigating from the second page back to the first. The first page had already been rendered once, but it needed to be re-rendered with different initial values.

The root cause of this error lies within Angular’s change detection mechanism. Let me explain how it works. After performing each operation, Angular stores the values it used for that operation in the component view’s oldValues property. Once all components have been checked, Angular initiates the next digest cycle. However, at this stage, instead of performing operations, it compares the current values with those stored from the previous cycle.

It’s important to note that this additional level of checking only occurs in development mode. Angular enforces a unidirectional data flow from the top of the component tree to the bottom. In other words, once a parent component’s changes have been processed, no child component should update the parent’s properties.

Now, let’s move on to possible solutions for resolving this error. One option is to use asynchronous updates, such as setTimeout, to defer the update until the next cycle. This allows Angular to process the changes without triggering the error.

Another solution involves manually triggering change detection at the ngAfterViewInit() lifecycle hook using the _changeDetectorRef.detectChanges() method. The ChangeDetectorRef class provides several methods for managing change detection, including markForCheck(), detach(), detectChanges(), checkNoChanges(), and reattach().

By utilizing these methods, you can manually run change detection and update the child view. Our colleague was pleased to find that by implementing this workaround, the error message was resolved.

In conclusion, if you come across the “ExpressionChangedAfterItHasBeenCheckedError” in your Angular development, remember that it’s essential to respect Angular’s unidirectional data flow. You can either use asynchronous updates or manually trigger change detection, and these methods should help you overcome the error.

That’s it for today’s episode of “Continuous Improvement.” We hope that sharing this experience and offering practical solutions will assist you in your own development journey.

If you found this episode helpful and would like to dive deeper into the world of continuous improvement, be sure to check out our blog and subscribe to our podcast for future episodes.

Remember, growth is a continuous process, and we’re here to support you every step of the way. Thanks for listening, and until next time, keep coding and keep improving.