I am using angularJS together with coffeescript. Why would the following $watch expression not trigger when state.title changes?
$scope.state = {title: 'Mr'}
$scope.$watch 'state', ()->console.log 'state changed!', true
The html to bind to this is simple:
<input type="text" ng-model="state.title"/>
PS: I solved this problem already, but it took me an hour. So thought I would share. The bug is visible in the code above, no other code is necessary.
That's because the way watch works is it does deep/dirty checking. Dirty checking is only done to see if the object reference has changed (which it hasn't in this case) deep checking would find out that the object has changed as one of the keys has updated.
I'm not familiar with coffeescript and I tend to stay away from it, but when I run your code through the interpreter this is what it spits out:
$scope.$watch('state', function() {
return console.log('state changed!', true);
});
Which is incorrect, I believe you want it to spit out:
$scope.$watch('state', function() {
return console.log('state changed!');
}, true);