top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is angular js $watch, $digest and $apply?

+2 votes
458 views

The AngularJS $scope functions $watch(), $digest() and $apply() are some of the central functions in AngularJS.​

$watch

The $scope.watch() function is used to observe changes in a variable on the $scope. It accepts three parameters : expression, listener and equality object where listener and equality object are optional parameters.

For Example:

$scope.$watch(function() {},
              function() {}
             );

$digest

The $scope.$digest() function iterates through all the watches in the $scope object, and its child $scope objects (if it has any). When $digest() iterates over the watches, it calls the value function for each watch. 

For Example:

//to update $scope
 $scope.$digest();

$apply

The $scope.$apply() function takes a function as parameter which is executed, and after that $scope.$digest() is called internally. That makes it easier for you to make sure that all watches are checked, and thus all data bindings refreshed.

For Example:

$scope.$apply(function() {
    $scope.data.myVar = "Another value";
});

Video

posted Jan 6, 2016 by anonymous

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...