3

example

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

The function starts when data will change but if I want to know what function cause to the change.

david lopez
  • 405
  • 2
  • 5
  • 16

1 Answers1

1

You can't directly interrogate the variable 'data' to check what method changed it and I'm not aware of $watch having an 'origin' style property.

So you'd need to track this yourself, so I'd recommend having an intermediary method which changes the actual data, and all others call this.

Said method could then store the source and then the watch would be triggered. But if doing that do you still need the watch?

Alternatively you could just set a flag denoting the origin.

You could also use evaluation in your watch statement as explained in an interesting article by Cameron Boehmer, that way you could only trigger your watch if certain other conditions (like flags) are met.

Rich
  • 970
  • 2
  • 16
  • 42