jhamm
jhamm

Reputation: 25022

Why is $watch not working like I expect - Angular

I am trying to watch a value in my controller. When it changes, I want to send out a broadcast, but I never get inside the $watch function. Here is the function:

  $scope.$watch($scope.selectedEncounter, function(selectedEncounter) {
    $scope.$broadcast('selecteRowChange', { encounter: selectedEncounter });
  });

Can I watch something attached to the scope? If so what is the issue I am having with this code. If not, how do I implement this code to work?

Upvotes: 1

Views: 97

Answers (2)

Vijay Pande
Vijay Pande

Reputation: 274

The object you are watching is a complex object. Hence you should set objectEquality to true in your code as follows:

$scope.$watch('selectedEncounter', function(selectedEncounter) {
// ....
}, true);

Notice the true value as last parameter to the $scope.$watch function at the end.

Upvotes: 1

Caio Cunha
Caio Cunha

Reputation: 23394

You should pass either a function or a property name to your $watch function.

So, in your case, you should just change your code to:

$scope.$watch('selectedEncounter', function(value) {
  // ...
});

Here is some more info from the docs.

Upvotes: 3

Related Questions