0

I have a Complex scope variable, something like the following.

$scope.ComplexVariable={
      PrimaryObjOne:{
         SecondaryOne:'',
         SecondaryTwo:''
      },
      PrimaryObjTwo:{
         SecondaryOne:'',
         SecondaryTwo:''
      }
}

Is there any clean way to check if any of the properties(at the secondary level in my example) associated with this object were modified. Adding a $watch against each property would work, but I was wondering if there was a cleaner way to do this.

Ritwik Sen
  • 296
  • 1
  • 13

2 Answers2

0
$scope.$watch("ComplexVariable", function (oldVal, newVal) {
}, true);

The true at the end will watch the entire object for any changes.

Scottie
  • 11,050
  • 19
  • 68
  • 109
0

The third argument to $scope.$watch allows you to compare objects using object equality rather than reference equality (which is the default).

function objectChanged() {
  // ...
}

var useObjectEquality = true;

$scope.$watch('ComplexVariable', objectChanged, useObjectEquality);

This makes use of angular.equals rather than == or ===.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120