ng-repeat不使用嵌套的json

时间:2016-08-25 00:55:32

标签: javascript angularjs json

我有以下json对象

$scope.receipts = {
    acquirer : [
        {
            id: 1,
            name: "REDE",
            balanceAmount: 4462.29,
            cardProducts: [
                {
                    cardProduct: {
                        balanceAmount: 2222,
                        id: 1,
                        name: "MASTERCARD CREDITO",
                        lancamentos: [ 
                            {
                                description: "vendas",
                                payedAmount: 1111   
                            },
                            {
                                description: "cancelamentos",
                                payedAmount: 1111
                            },
                            {
                                description: "ajustes",
                                payedAmount: 1111
                            }
                        ]
                    }
                }
            ]
        }
    ]
};

我正在尝试使用angular ng-repeat

创建一个嵌套表
<tbody ng-repeat="i in receipts.acquirer[0].cardProducts[0]">
            <tr class="group">
                <th></th>
                <th>lançamento</th>
                <th>valor líquido (R$)</th>
                <th></th>
            </tr>

            <tr>
                <th class="except"></th>

                <th>
                    <div class="cardFlag brand_{{i.id}}"></div>
                    {{ i.name }} {{ i.lancamentos[0].teste.description }}
                </th>
                <th>{{i.balanceAmount | currency}}</th>
                <th><a href="#/receipts/details">detalhes</a></th>
            </tr>
            <ng-repeat ng-repeat="e in i.lancamentos[0]">
                <tr class="inset">
                    <th></th>
                    <th class="inset-1">{{ e.description }}</th>
                    <th>{{e.payedAmount}} </th>
                    <th></th>
                </tr>
            </ng-repeat>
        </tbody>

我的第一个ng-repeat工作得非常好。我的问题实际上是我的第二个ng-repeat,它没有输出任何内容。

处理这种嵌套json并让ng-repeat处理“ lancamentos ”的更好方法是什么?

4 个答案:

答案 0 :(得分:3)

由于您在lancamentos []内部没有id属性,请将您的foreach更改为

Interval

答案 1 :(得分:0)

不应该在ng-repeat上继续使用tr吗?并且不要说i.lancamentos[0],因为那只是第一个。

<tr class="inset" ng-repeat="e in i.lancamentos[0]">
    <th></th>
    <th class="inset-1">{{ e.description }}</th>
    <th>{{e.payedAmount}} </th>
    <th></th>
</tr>

答案 2 :(得分:0)

基于此对象:

{
    acquirer : [
        {
            id: 1,
            name: "REDE",
            balanceAmount: 4462.29,
            cardProducts: [
                {
                    cardProduct: {
                        balanceAmount: 2222,
                        id: 1,
                        name: "MASTERCARD CREDITO",
                        lancamentos: [ 
                            {
                                description: "vendas",
                                payedAmount: 1111   
                            },
                            {
                                description: "cancelamentos",
                                payedAmount: 1111
                            },
                            {
                                description: "ajustes",
                                payedAmount: 1111
                            }
                        ]
                    }
                }
            ]
        }
    ]
};

这个区块:

<tbody ng-repeat="i in receipts.acquirer[0].cardProducts[0]">

将i替换为此对象:

{
    cardProduct: {
        balanceAmount: 2222,
        id: 1,
        name: "MASTERCARD CREDITO",
        lancamentos: [
            {
                description: "vendas",
                payedAmount: 1111   
            },
            {
                description: "cancelamentos",
                payedAmount: 1111
            },
            {
                description: "ajustes",
                payedAmount: 1111
            }
        }
    }
}

这意味着这个块:

<ng-repeat ng-repeat="e in i.lancamentos[0]">

实际上需要:

<ng-repeat ng-repeat="e in i.cardProduct.lancamentos">

编辑:正如在其他一些注释中所观察到的那样,假设acquirer.cardProducts数组中可能有多个元素,您需要通过删除[0]来修改第一个ng-repeat。

<tbody ng-repeat="i in receipts.acquirer[0].cardProducts">
祝你好运!

答案 3 :(得分:0)

只需改变一下:

<ng-repeat ng-repeat="e in i.lancamentos[0]">
   <tr class="inset">
      <th></th>
      <th class="inset-1">{{ e.description }}</th>
      <th>{{e.payedAmount}} </th>
      <th></th>
   </tr>
</ng-repeat>

进入这个:

<tr class="inset" ng-repeat="e in i.lancamentos">
   <th></th>
   <th class="inset-1">{{ e.description }}</th>
   <th>{{e.payedAmount}} </th>
   <th></th>
</tr>