mcv*_*mcv 0 javascript angularjs
我在 AngularJS 中遇到了一个问题,$scope.$watchCollection()会引发错误。我已经将我的代码减少到与文档中的示例代码完全相同的程度(http://docs.angularjs.org/api/ng .$rootScope.Scope#$watchCollection),并且仍然抛出错误:
function OverviewCtrl($scope) {
$scope.names = ['igor', 'matias', 'misko', 'james'];
$scope.dataCount = 4;
$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
});
}
Run Code Online (Sandbox Code Playgroud)
我收到错误
'undefined' is not a function (evaluating '$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
})')
Run Code Online (Sandbox Code Playgroud)
我不知道可能是什么问题。我正在按照文档说的做,除了我将它放在控制器中,但似乎这段代码是用于控制器的。那么这里有什么问题呢?
您还可以使用以下语法:
$scope.$watch('myCollection', function(value, oldValue) {
// insert awesome code here
}, true);
Run Code Online (Sandbox Code Playgroud)
该true参数告诉 AngularJS“深入观察”该值。