Thursday, December 31, 2015

AngularJs scope.$watch, $watchgroup and $watchCollection

$watch service helps us to do something or run any business logic when any property or variable value attached to $scope changed



$watch(variable, listener)
function MyController($scope) {

   $scope.myVar = 1;

   $scope.$watch('myVar', function() {
       alert('hey, myVar has changed!');
   });

   $scope.buttonClicked = function() {
      $scope.myVar = 2; // This will trigger $watch expression to kick in
   };

}

There are some other additional angular service $watchCollection and $watchgroup .

Scope.$ watchgroup: it helps us to watch a set of group of variables together , if anyone of them is changed, it will trigger $watchGroup expression.

$watchGroup(watchExpression, listener)

Scope.$watchCollection : it helps us to watch whole object’s properties , we need to pass object instead of each properties of object.


No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...