在AngularJS重复部分功能调用多次 [英] Function Called Multiple Times in AngularJS Repeat Section
问题描述
我遇到,我想绑定到一个函数的输出NG重复循环中的一个问题。我发现功能正在每个项目两次而非一次是我期望调用。这里的NG-重复部分(注意在最后的calcRowTotal()调用):
I'm running into an issue where I want to bind to the output of a function inside of an ng-repeat loop. I'm finding that the function is being called twice per item rather than once as I'd expect. Here's the ng-repeat section (notice the calcRowTotal() call at the end):
<tr ng-repeat="row in timesheetRows">
<td>
<select ng-model="row.categoryID">
<option ng-repeat="category in categories" value="{{category.id}}">
{{category.title}}
</option>
</select>
</td>
<td ng-repeat="day in row.dayValues">
<input type="text" ng-model="day.hours" />
</td>
<td>{{calcRowTotal($index, row)}}</td>
</tr>
该calcRowTotal()函数图所示:
The calcRowTotal() function is shown next:
$scope.calcRowTotal = function (index, row) {
console.log('calcRowTotal - Index: ' + index);
var total = 0;
for (var i = 0; i < row.dayValues.length; i++) {
var num = parseFloat(row.dayValues[i].hours);
if (isNaN(num)) {
num = 0;
//this.dayValues[i].hours = 0;
}
total += num;
}
//updateDayTotals();
return total;
}
的项目之一的一个例子被迭代通过示出下一个:
An example of one of the items being iterated through is shown next:
{
categoryID: 2,
dayValues: [
{ day: $scope.days[0], hours: 5 },
{ day: $scope.days[1], hours: 0 },
{ day: $scope.days[2], hours: 3 },
{ day: $scope.days[3], hours: 0 },
{ day: $scope.days[4], hours: 2 },
{ day: $scope.days[5], hours: 5 },
{ day: $scope.days[6], hours: 8 }
]
}
我看到在控制台下面(两项是我目前通过循环集合中):
I'm seeing the following in the console (two items are currently in the collection I'm looping through):
calcRowTotal - Index: 0
calcRowTotal - Index: 1
calcRowTotal - Index: 0
calcRowTotal - Index: 1
我当然可以做一个rowTotal属性,但将preFER结合上面所显示的功能提供实时的数据。希望复制是一些简单的我失踪,所以我AP preciate任何反馈为什么我看到的重复。作为一个方面说明,如在文本框变化的一个数据,我需要更新的行总数,以及因此它可能是我需要一个不同的方法。有兴趣了解首先这种特定的情况虽然....绝对不想重复,因为可能有很多行可能。
I could certainly make a "rowTotal" property but would prefer to bind to "live" data provided by the function shown above. Hopefully the duplication is something simple I'm missing so I appreciate any feedback on why I'm seeing the duplication. As a side note, as data in one of the textboxes changes I need to update the row totals as well so it may be I need a different approach. Interested in understanding this particular situation first though....definitely don't want the duplication because there could be a lot of rows potentially.
下面是一个例子: http://jsfiddle.net/dwahlin/Y7XbY/2/
推荐答案
这是因为你绑定到一个函数前pression这里:
It's because you're binding to a function expression here:
<td>{{calcRowTotal($index, row)}}</td>
什么是它迫使该功能将每个项目的重新评估,在每个摘要。你会想要做的prevent是pre-计算该值并把它放在你的数组中开始。
What that does it force that function to be reevaluated on every item, on every digest. What you'll want to do to prevent that is pre-calculate that value and put it in your array to begin with.
做到这一点的方法之一就是建立阵列上的手表:
One way to do that is to set up a watch on your array:
$scope.$watch('timesheetRows', function(rows) {
for(var i = 0; i < value.length; i++) {
var row = rows[i];
row.rowTotal = $scope.calcRowTotal(row, i);
}
}, true);
然后,所有你所要做的就是绑定到新的值:
Then all you have to do is bind to that new value:
<td>{{row.rowTotal}}</td>
这篇关于在AngularJS重复部分功能调用多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!