Watch multiple $scope attributes Watch multiple $scope attributes angularjs angularjs

Watch multiple $scope attributes


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 });


Beginning with AngularJS 1.1.4 you can use $watchCollection:

$scope.$watchCollection('[item1, item2]', function(newValues, oldValues){    // do stuff here    // newValues and oldValues contain the new and respectively old value    // of the observed collection array});

Plunker example here

Documentation here


$watch first parameter can also be a function.

$scope.$watch(function watchBothItems() {  return itemsCombinedValue();}, function whenItemsChange() {  //stuff});

If your two combined values are simple, the first parameter is just an angular expression normally. For example, firstName and lastName:

$scope.$watch('firstName + lastName', function() {  //stuff});