Skip to content

angular $watch

Sébastien LUCAS edited this page Mar 31, 2014 · 7 revisions

True : Deep comparison (calculation expansive)
Default false

$scope.$watch('form', function(newVal, oldVal){
    console.log('changed');
}, true);

Tutos

When things happen "inside Angular" – e.g., you type into a textbox that has Angular two-way databinding enabled (i.e., uses ng-model), an $http callback fires, etc. – $apply has already been called, so we're inside the "AngularJS" rectangle in the figure above. All watchExpressions will be evaluated (possibly more than once – until no further changes are detected).

When things happen "outside Angular" – e.g., you used bind() in a directive and then that event fires, resulting in your callback being called, or some jQuery registered callback fires – we're still in the "Native" rectangle. If the callback code modifies anything that any $watch is watching, call $apply to get into the AngularJS rectangle, causing the $digest loop to run, and hence Angular will notice the change and do its magic.

WIKI by Sébastien Lucas CEO & Funder or Bricks

Clone this wiki locally