Posts

Showing posts from July 24, 2016

AngularJS $watch() , $digest() and $apply()

AngularJS $watch() , $digest() and $apply() When you create a data binding in your view to a variable on the $scope object, AngularJS creates a "watch" internally. A watch means that AngularJS watches changes in the variable on the $scope object. These watches are created using the $scope.$watch() function. When you register a watch you pass two functions as parameters to the $watch() function: * A value function * A listener function Example : $scope.$watch(function() {}, function() {} ); The value function should return the value which is being watched. AngularJS can then check the value returned(current value) against the last calculated value. That way AngularJS can determine if the value has changed. If yes, then the corresponding listener function executes. Example 1: $scope.$watch(function(scope) { return scope.data.myVar }, function() {} ); Example 2: $scope.$watch(function(scope) { return scope.da