Watch multiple $scope attributes

Watch multiple $scope attributes

Is there a way to subscribe to events on multiple objects using $watch
$scope.$watch(‘item1, item2’, function () { });


Solution 1:

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

$ = 'foo';
$ = '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] -> $ 
  // and 
  // newValues[1] -> $ 

Solution 2:

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

Solution 3:

$watch first parameter can also be a function.

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

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() {

Solution 4:

Here’s a solution very similar to your original pseudo-code that actually works:

$scope.$watch('[item1, item2] | json', function () { });

EDIT: Okay, I think this is even better:

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

Basically we’re skipping the json step, which seemed dumb to begin with, but it wasn’t working without it. They key is the often omitted 3rd parameter which turns on object equality as opposed to reference equality. Then the comparisons between our created array objects actually work right.

Solution 5:

You can use functions in $watchGroup to select fields of an object in scope.

        [function () { return _this.$scope.ViewModel.Monitor1Scale; },   
         function () { return _this.$scope.ViewModel.Monitor2Scale; }],  
         function (newVal, oldVal, scope) 
             if (newVal != oldVal) {

Solution 6:

Why not simply wrap it in a forEach?

angular.forEach(['a', 'b', 'c'], function (key) {
  scope.$watch(key, function (v) {

It’s about the same overhead as providing a function for the combined value, without actually having to worry about the value composition.