Question

Is it correct that AngularJS creates a watch for EVERY element that belongs to a $scope? I'm asking because I'd like to know whether ALL $scope variables get checked whenever the method $scope.$apply() is called.

I read somewhere that watches are only created if a $scope variable was referenced in the view, which is quite confusing to me, as I have $scope variables that get "digested" even tough they are not directly referenced in the view.

Was it helpful?

Solution

All 'live' scope variables/functions get 'dirty checked' during the digest cycle.

A scope variable doesnt have to be interpolated into the view to be useful. Some other directive can use scope inheritance to access this scope variable or something can $watch this scope variable and then respond to the changes.

The only way to get scope to not be dirty check is to destroy it by placing it behind ng-if, ng-switch or by explicitly deleting. it.

OTHER TIPS

All scope variables are checked whenever $apply is called. The only way you can (possibly) bypass this checking would be if you alter some variable outside angular's scope.

As you can see with this $timeout vs setTimeout example:

http://plnkr.co/edit/C3n6ipl0ZstfKSY8oWBx?p=preview

// $scope.model.noByPass is updated after 1 second in the view.
$timeout(function() {
    $scope.model.noByPass = 'I\'m a string now!';
}, 1000);

// As this triggers outside angular's scope, the view does not update
// to show this change.
window.setTimeout(function() {
    $scope.model.byPass = 'I\'m a string now!';
}, 2000);

// This triggers a view change and now the view acknowledges this and
// the previous change.
$timeout(function() {
    $scope.model.noByPass = 'Issuing another check';
}, 3000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top