Javascript ng repeat中的函数保持循环
我正在使用Javascript ng repeat中的函数保持循环,javascript,angularjs,Javascript,Angularjs,我正在使用ng repeat加载项目列表。在其中一个字段中,我将从ng repeat获取的ID传递给一个函数,以从另一个数据库表获取信息。当我这样做并console.log结果时,它只是不断循环 <ion-slide-box ng-repeat="q in questions.list track by $index"> <ion-slide class="default box"> <div class="amount">{{ g
ng repeat
加载项目列表。在其中一个字段中,我将从ng repeat
获取的ID
传递给一个函数,以从另一个数据库表获取信息。当我这样做并console.log
结果时,它只是不断循环
<ion-slide-box ng-repeat="q in questions.list track by $index">
<ion-slide class="default box">
<div class="amount">{{ getAmountQuestion(q.guid) }}</div>
<div class="question"><h5>{{ q.fields.question }}</h5></div>
</ion-slide>
</ion-slide-box>
$scope.getQuestionAmount = function(id) {
QuestionsService.getAllVotesById(id).then(function (data) {
console.log(data.count);
});
};
{{getAmountQuestion(q.guid)}
{{q.fields.question}
$scope.getQuestionAmount=函数(id){
QuestionsService.getAllVotesById(id).then(函数(数据){
console.log(data.count);
});
};
将API服务调用放入watchExpression
中是一个非常糟糕的想法,因为AngularJS框架在每个摘要周期调用该表达式
从文档中:
每次调用时都会调用watchExpression
,并应返回将被监视的值。(watchExpression
在使用同一输入执行多次时不应更改其值,因为它可能由执行多次。也就是说,watchExpression
应为
--
重构代码以在控制器内执行API调用,缓存结果,并在watch表达式中使用类似于
{{cachedAmountQuestion[q.guid]}
有多少元素在questions.list中?您能提供plnkr吗?仅出于性能原因,我就重写了getQuestionAmount()
-函数和后端REST服务从所有问题中获取GUID并返回所有投票,因为在当前设置中,它将运行一个循环,并为每个问题调用REST服务。@Greg Johannes Jander是对的。但是,无论如何,您会得到比questions.list中的项更多的console.logs吗?您不能在视图中执行类似的操作。是吗摘要周期运行了很多次,所以你的请求数量非常多,导致更多的摘要出现,请求数量也非常多requests@charlietfl由于他没有给作用域分配任何内容,我怀疑他的代码会触发更多的摘要周期你应该把它变成一个答案。