Tony Ennis
Tony Ennis

Reputation: 12309

My $watch isn't firing

I have what I think is a garden-variety $watch set up, but it sure isn't working.

Here's a fiddle. When you click the buttons, the fac.backingFabric attribute will be updated - you can see it happening due to the two-way binding. But the $watch isn't happening. What am I doing wrong?

Upvotes: 0

Views: 102

Answers (1)

PSL
PSL

Reputation: 123739

You watch has no issues it is firing properly. But you have issues inside the watcher. You are looking for undefined variable fac where you should be looking for $scope.fac.

$scope.$watch("fac.backingFabric", function () {
    alert($scope.fac.backingFabric);
    $scope.out = "out" + $scope.fac.backingFabric;
});

Infact you can look for the first argument of the watch function.

$scope.$watch("fac.backingFabric", function (value) {
     $scope.out = "out" + value;
});

Upvotes: 1

Related Questions