更新列表中的ng-repeat项时,仅更新最后一项

时间:2018-07-05 13:04:33

标签: angularjs

我有一些数字的ng-repeat列表。 我也对所有数字求和,并在表的页脚中显示和。

现在,我需要手动将数字输入SUM时,计算ng-repat中每个项目的数字。

在这里,我在ng-repat中调用ng-init并将$ index和item传递给函数     计算总和

<tr ng-repeat="item in myCartItems track by $index" ng- 
init="storeValueOnScope($index, item)">


$scope.storeValueOnScope = function(index, item){
  $rootScope.indexOdabranihKlupa = index;
  $rootScope.impresijeKampanja = item.campaignImpressions;
}

最大展示次数与     所选的展示次数

$scope.getUserSelectedImpresion = function (impressions){
   $scope.userSelectedImpressionsSumBench = impressions;

   $scope.percentageDiff = $scope.userSelectedImpressionsSumBench  / 
     $rootScope.totaImpressionsSum * 100;

   $scope.calculateDiff($rootScope.indexOdabranihKlupa, 
     $rootScope.impresijeKampanja);
}

在这里,我计算新数字,我想在已经加载到DOM中的ng-repeat中放置新数字

$scope.calculateDiff = function(index, item) {
  $scope.myCartItems[index].campaignImpressions = (item * 
    $scope.percentageDiff) / 100; 
}

这是我想要获得新价值的部分

<input type="number" ng-model="item.campaignImpressions" ng-min="0" ng- 
max="item.maxValue" class="inputPolje" name="campaignImpressions" />

问题

当我计算所有这些时,我得到正确的数字,但仅适用于最后一项,而不适用于ng-repeat列表中的每一项

 |ID   | Available impressions (input) |
 |1000 |       5000                    | max: 5000
 |1004 |       5000                    | max: 5000
 |1098 |       4666.666666666666       | max: 5000 //only this one is calculated
 total max: 15000 (input field)
 user select: 14000 //this is number for make calculation

这是我的json示例

[{
"id": 1000,
"campaignImpressions": 5000,
"mediaImpressions": 5000,
"time": "20:30:00"
 }, {
"id": 1004,
"campaignImpressions": 5000,
"mediaImpressions": 5000,
"time": "20:30:00"
 }, {
"id": 1004,
"campaignImpressions": 5000,
"mediaImpressions": 5000,
"time": "20:30:00"
 }]

0 个答案:

没有答案