How does AngularJS know when variables change? How does AngularJS dirty checking work?

angularjs, javascript, observer-pattern

Solution

From this link:

Angular defines a concept of a so called digest cycle. This cycle can be considered as a loop, during which Angular checks if there are any changes to all the variables watched by all the $scopes. So if you have $scope.myVar defined in your controller and this variable was marked for being watched, then you are explicitly telling Angular to monitor the changes on myVar in each iteration of the loop.

This "`digest`" is also called "dirty checking", because, in a way, it scans the scope for changes. I cannot say if it's for better or for worse than observable pattern. It depends on your needs.

Some links:

- Angular documentation

- A blog post about Angular scopes

Problem

I was reading some article to understand a little bit more how AngularJS works. One of the terms that I didn't understand is `Dirty Checking`. What is it exactly? It seems like the Observer pattern but apparently it's better. Can you help me understand this please? EDIT : it can be also useful for people who wants to learn more about that to watch this video from swiip at NgEurope some years ago.

Original source

Related problems