Angularjs 为什么下面的角度表表达式不起作用?
我正在使用angularJS和coffeescript。为什么state.title更改时,以下$watch表达式不会触发Angularjs 为什么下面的角度表表达式不起作用?,angularjs,coffeescript,Angularjs,Coffeescript,我正在使用angularJS和coffeescript。为什么state.title更改时,以下$watch表达式不会触发 $scope.state = {title: 'Mr'} $scope.$watch 'state', ()->console.log 'state changed!', true 与此绑定的html很简单: <input type="text" ng-model="state.title"/> 我已经解决了这个问题,但花了我一个小时。所以我想我会
$scope.state = {title: 'Mr'}
$scope.$watch 'state', ()->console.log 'state changed!', true
与此绑定的html很简单:
<input type="text" ng-model="state.title"/>
我已经解决了这个问题,但花了我一个小时。所以我想我会分享。在上面的代码中可以看到该错误,不需要其他代码。这是因为watch的工作方式是进行深度/脏检查。脏检查仅用于查看对象引用是否已更改(在本例中没有更改),深入检查将发现对象已更改,因为其中一个键已更新 我不熟悉coffeescript,我倾向于远离它,但当我通过解释器运行代码时,它会说:
$scope.$watch('state', function() {
return console.log('state changed!', true);
});
这是不正确的,我相信你希望它吐出来:
$scope.$watch('state', function() {
return console.log('state changed!');
}, true);
这是正确的。我对参数传递感到困惑,所以“true”转到控制台日志而不是$watch。