Watch multiple $scope attributes Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Watch multiple $scope attributes without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Is there a way to subscribe to events on multiple objects using $watch

E.g.

$scope.$watch('item1, item2', function () { });

Answer

Starting from AngularJS 1.3 there’s a new method called $watchGroup for observing a set of expressions.

$scope.foo = 'foo';
$scope.bar = 'bar';

$scope.$watchGroup(['foo', 'bar'], function(newValues, oldValues, scope) {
  // newValues array contains the current values of the watch expressions
  // with the indexes matching those of the watchExpression array
  // i.e.
  // newValues[0] -> $scope.foo 
  // and 
  // newValues[1] -> $scope.bar 
});
We are here to answer your question about Watch multiple $scope attributes - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji