Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 为什么下面的角度表表达式不起作用?_Angularjs_Coffeescript - Fatal编程技术网

Angularjs 为什么下面的角度表表达式不起作用?

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"/> 我已经解决了这个问题,但花了我一个小时。所以我想我会

我正在使用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"/>


我已经解决了这个问题,但花了我一个小时。所以我想我会分享。在上面的代码中可以看到该错误,不需要其他代码。

这是因为watch的工作方式是进行深度/脏检查。脏检查仅用于查看对象引用是否已更改(在本例中没有更改),深入检查将发现对象已更改,因为其中一个键已更新

我不熟悉coffeescript,我倾向于远离它,但当我通过解释器运行代码时,它会说:

$scope.$watch('state', function() {
  return console.log('state changed!', true);
});    
这是不正确的,我相信你希望它吐出来:

$scope.$watch('state', function() {
  return console.log('state changed!');
}, true);    

这是正确的。我对参数传递感到困惑,所以“true”转到控制台日志而不是$watch。