我想在ng-repeat属性中调用函数,这是我的代码
HTML
<body ng-controller="mainCtrl"> <div ng-repeat='item in getGroupedRange() track by item.id'> <span>{{item.val}}</span> <span>{{item.abs}}</span> <span>{{item.rel}}</span> <span>{{item.cum}}</span> </div> </body>
JS
$scope.getGroupedRange = function() { return [ { val: 1,abs: 1,rel: 1,cum: 1,id: 123456 } ]; };
当我打开控制台时,我注意到了错误
10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: [[{"msg":"fn: function (c,d,e,f){e=a(c,f);return b(e,c,d)}","newVal":9,"oldVal":8}],[{"msg":"fn: function (c,"newVal":10,"oldVal":9}],"newVal":11,"oldVal":10}],"newVal":12,"oldVal":11}],"newVal":13,"oldVal":12}]]
不,你不能像这样在ngRepeat中使用函数.问题是Angular在摘要循环中使用严格的对象比较来确定自上次检查后属性的值是否发生了变化.那么,每次调用getGroupedRange都会返回新值(新数组). Angular不知道并认为这个值不稳定,因此继续检查.但它在10次检查后中止.
原文链接:https://www.f2er.com/angularjs/143342.html您需要构造必要的数组并将其分配给scope属性,因此在摘要循环期间不会更改:
$scope.groupedRange = $scope.getGroupedRange();
然后像在ngRepeat中一样使用它
<div ng-repeat='item in groupedRange track by item.id'> <span>{{item.val}}</span> <span>{{item.abs}}</span> <span>{{item.rel}}</span> <span>{{item.cum}}</span> </div>