admin管理员组文章数量:1026380
I've got a problem in AngularJS where $scope.$watchCollection()
throws an error. I've reduced my code to the point where it's exactly the same as example code in the docs (.$rootScope.Scope#$watchCollection), and the error is still thrown:
function OverviewCtrl($scope) {
$scope.names = ['igor', 'matias', 'misko', 'james'];
$scope.dataCount = 4;
$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
});
}
I get the error
'undefined' is not a function (evaluating '$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
})')
I have no idea what the problem could possibly be. I'm doing exactly what the docs say, except I'm putting it in a controller, but it seems this code is intended for use in controllers. So what's the problem here?
I've got a problem in AngularJS where $scope.$watchCollection()
throws an error. I've reduced my code to the point where it's exactly the same as example code in the docs (http://docs.angularjs/api/ng.$rootScope.Scope#$watchCollection), and the error is still thrown:
function OverviewCtrl($scope) {
$scope.names = ['igor', 'matias', 'misko', 'james'];
$scope.dataCount = 4;
$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
});
}
I get the error
'undefined' is not a function (evaluating '$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
})')
I have no idea what the problem could possibly be. I'm doing exactly what the docs say, except I'm putting it in a controller, but it seems this code is intended for use in controllers. So what's the problem here?
Share Improve this question asked Oct 4, 2013 at 10:12 mcvmcv 4,4596 gold badges37 silver badges41 bronze badges 8-
1
What version of angular are you using?
$watchCollection
is a fairly new feature. – Jussi Kosunen Commented Oct 4, 2013 at 10:16 - 1.0.6. The latest stable version seems to be 1.0.8. – mcv Commented Oct 4, 2013 at 10:17
- upgrading to 1.2.0-rc2 seems to do the trick. Also had to explicitly include routeProvider to get the app working. (I wonder if that means I can drop routeProvider pletely; I don't really want my views directly tied to urls.) – mcv Commented Oct 4, 2013 at 10:32
-
Yeah
$watchCollection
isn't in the stable builds (you can specify your version in the API documentation in the top left to see what you can use). Not sure about yourrouteProvider
woes, though. – Jussi Kosunen Commented Oct 4, 2013 at 10:34 -
1
Not the best place for this, but just a note on routes. You can only have one
ng-view
, which is why I skippedrouteProvider
and just useng-include
s. – Jussi Kosunen Commented Oct 4, 2013 at 11:45
1 Answer
Reset to default 5You could also use the following syntax :
$scope.$watch('myCollection', function(value, oldValue) {
// insert awesome code here
}, true);
The true
parameter tells AngularJS to "deepwatch" the value.
I've got a problem in AngularJS where $scope.$watchCollection()
throws an error. I've reduced my code to the point where it's exactly the same as example code in the docs (.$rootScope.Scope#$watchCollection), and the error is still thrown:
function OverviewCtrl($scope) {
$scope.names = ['igor', 'matias', 'misko', 'james'];
$scope.dataCount = 4;
$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
});
}
I get the error
'undefined' is not a function (evaluating '$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
})')
I have no idea what the problem could possibly be. I'm doing exactly what the docs say, except I'm putting it in a controller, but it seems this code is intended for use in controllers. So what's the problem here?
I've got a problem in AngularJS where $scope.$watchCollection()
throws an error. I've reduced my code to the point where it's exactly the same as example code in the docs (http://docs.angularjs/api/ng.$rootScope.Scope#$watchCollection), and the error is still thrown:
function OverviewCtrl($scope) {
$scope.names = ['igor', 'matias', 'misko', 'james'];
$scope.dataCount = 4;
$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
});
}
I get the error
'undefined' is not a function (evaluating '$scope.$watchCollection('names', function(newNames, oldNames) {
$scope.dataCount = newNames.length;
})')
I have no idea what the problem could possibly be. I'm doing exactly what the docs say, except I'm putting it in a controller, but it seems this code is intended for use in controllers. So what's the problem here?
Share Improve this question asked Oct 4, 2013 at 10:12 mcvmcv 4,4596 gold badges37 silver badges41 bronze badges 8-
1
What version of angular are you using?
$watchCollection
is a fairly new feature. – Jussi Kosunen Commented Oct 4, 2013 at 10:16 - 1.0.6. The latest stable version seems to be 1.0.8. – mcv Commented Oct 4, 2013 at 10:17
- upgrading to 1.2.0-rc2 seems to do the trick. Also had to explicitly include routeProvider to get the app working. (I wonder if that means I can drop routeProvider pletely; I don't really want my views directly tied to urls.) – mcv Commented Oct 4, 2013 at 10:32
-
Yeah
$watchCollection
isn't in the stable builds (you can specify your version in the API documentation in the top left to see what you can use). Not sure about yourrouteProvider
woes, though. – Jussi Kosunen Commented Oct 4, 2013 at 10:34 -
1
Not the best place for this, but just a note on routes. You can only have one
ng-view
, which is why I skippedrouteProvider
and just useng-include
s. – Jussi Kosunen Commented Oct 4, 2013 at 11:45
1 Answer
Reset to default 5You could also use the following syntax :
$scope.$watch('myCollection', function(value, oldValue) {
// insert awesome code here
}, true);
The true
parameter tells AngularJS to "deepwatch" the value.
本文标签: javascriptAngularJS watchCollection does not work throws errorStack Overflow
版权声明:本文标题:javascript - AngularJS: $watchCollection does not work; throws error - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745643175a2160868.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论