角度js中的嵌套ng重复失败

时间:2014-10-28 18:23:47

标签: angularjs

尝试进行嵌套ng-repeat,父ng-repeat工作但不适用于其子项 1.在app init时处理对象并插入数据 2.通过调用ng-repeat

显示数据
angular.module('lipapp', []).controller('lipapp_Module_Control', function ($scope, $http, $window) {
        $scope.CompaignBasket = []; 
        $scope.CompaignBasketTotal = 0;
        $scope.CompaignBasketCauseTotal = [];
        //Sample Data Section, Real data plz go to $scope.UpdateCompaign function
        $scope.InitialCompaignBasket = function () {

            $scope.CompaignBasket = [];
            var causeAmount = [];
            var causeitem = 0;

            var donorDetail = {}; 
            donorDetail.Date = '2/14/13';
            donorDetail.Donor = 'George Smith';
            donorDetail.City = 'Los Angelos';
            donorDetail.State = 'CA';
            donorDetail.Total = 400;

            causeAmount = [];  //forloop Dynamic Insert!
           causeAmount.push(10);
           causeAmount.push(0);  
           causeAmount.push(500);
           causeAmount.push(0);
           causeAmount.push(1000);
           causeAmount.push(0);
          //causeitem = 0;causeAmount.push(causeitem); or I should use this format?

            donorDetail.DonorCauseAmount = causeAmount;
            $scope.CompaignBasketTotal += donorDetail.Total;
            $scope.CompaignBasket.push(donorDetail);

            console.log( $scope.CompaignBasket);
            ...
          }

所有结果都正确显示但item.donorCauseAmount有抛出错误

             <tr ng-repeat="item in CompaignBasket">
                    <td>{{item.Date}}</td>
                    <td>{{item.Donor}}</td>
                    <td>{{item.City}}</td>
                    <td>{{item.State}}</td> 
                    <td ng-repeat="cause_item in item.DonorCauseAmount">{{cause_item}} </td>
                    <td>${{item.Total|number :0}}</td>
                </tr>

此外,当我尝试计算脚本中同一列中不同CauseItem的总数时,还会出现转发器错误

         for(var i=0; i< $scope.CompaignBasket.length; i++)
            { 

                if (i == 0)
                {
                    $scope.CauseTotalAmount = $scope.CompaignBasket[i].DonorCauseAmount;
                    console.log($scope.CauseTotalAmount);
                }
                else {
                    for (var j = 0; j < $scope.CauseTotalAmount.length; j++) {
                        //  $scope.CauseTotalAmount[j] += $scope.CompaignBasket[i].DonorCauseAmount[j];
                        console.log('total '+ $scope.CompaignBasket[i].DonorCauseAmount[j]); //Error show up by this!!!
                    }
                }
                console.log($scope.CauseTotalAmount);
            }

我该如何解决?

1 个答案:

答案 0 :(得分:5)

如果数组中有重复值,则需要按索引使用轨道。

<td ng-repeat="cause_item in item.DonorCauseAmount track by $index">{{cause_item}} </td>