javascript - AngularJS: $watchCollection does not work; throws error - Stack Overflow

I've got a problem in AngularJS where $scope.$watchCollection() throws an error. I've reduced

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 your routeProvider 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 skipped routeProvider and just use ng-includes. – Jussi Kosunen Commented Oct 4, 2013 at 11:45
 |  Show 3 more ments

1 Answer 1

Reset to default 5

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745643177a4637813.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信