By dirty checking the $scope object
Angular maintains a simple array of watchers in the $scope objects. If you inspect any $scope you will find that it contains an array called $$watchers.
Each watcher is an object that contains among other things
- An expression which the watcher is monitoring. This might just be an attribute name, or something more complicated.
- A last known value of the expression. This can be checked against the current computed value of the expression. If the values differ the watcher will trigger the function and mark the $scope as dirty.
- A function which will be executed if the watcher is dirty.
How watchers are defined
There are many different ways of defining a watcher in AngularJS.
You can explicitly $watch an attribute on $scope.
$scope.$watch('person.username', validateUnique);
You can place a {{}} interpolation in your template (a watcher will be created for you on the current $scope).
<p>username: {{person.username}}</p>
You can ask a directive such as ng-model to define the watcher for you.
<input ng-model="person.username />
The $digest cycle checks all watchers against their last value
When we interact with angular through the normal channels (ng-model, ng-repeat, etc) a digest cycle will be triggered by the directive.
A digest cycle is a depth first traversal of $scope and all its children. For each $scope object, we iterate over its $$watchers array and evaluate all the expressions. If the new expression value is different from the last known value, the watcher's function is called. This function might recompile part of the DOM, recompute a value on $scope, trigger an AJAX request, anything you need it to do.
Every scope is traversed and every watch expression evaluated and checked against the last value.
If a watcher is triggered, the $scope is dirty
If a watcher is triggered, the app knows something has changed, and the $scope is marked as dirty.
Watcher functions can change other attributes on $scope or on a parent $scope. If one $watcher function has been triggered, we can't guarantee that our other $scopes are still clean, and so we execute the entire digest cycle again.
If the $digest is dirty, we execute the entire $digest cycle again
We continually loop through the $digest cycle until either the digest cycle comes up clean (all $watch expressions have the same value as they had in the previous cycle), or we reach the digest limit. By default, this limit is set at 10.
If we reach the digest limit Angular will raise an error in the console:
10 $digest() iterations reached. Aborting!
The digest is hard on the machine but easy on the developer
As you can see, every time something changes in an Angular app, Angular will check every single watcher in the $scope hierarchy to see how to respond. For a developer this is a massive productivity boon, as you now need to write almost no wiring code, Angular will just notice if a value has changed.
From the perspective of the machine though this is wildly inefficient and will slow our app down if we create too many watchers. Misko has quoted a figure of about 4000 watchers before your app will feel slow on older browsers.
This limit is easy to reach if you ng-repeat over a large JSON array for example. You can mitigate against this using features like one-time binding to compile a template without creating watchers.