[编辑:已解决,但我认为有更好的方法,如果您找到它,我仍然很感兴趣]

我有银行清单。
每个银行都包含一个帐户列表。
每个帐户都包含一个操作列表。

我想在表格中显示所有操作。

银行清单如下所示:

$scope.banks = [
    {
        id: 1,
        name: 'bank_1',
        accounts: [
            {
                id: 1,
                name: 'account_1',
                operations: [
                    {id: 1, name:'operation_1', price:100},
                    {id: 2, name:'operation_2', price:200},
                    {id: 3, name:'operation_3', price:300},
                    {id: 4, name:'operation_4', price:400}
                ]
            },
            {
                id: 2,
                name: 'account_2',
                operations: [
                    {id: 5, name:'operation_5', price:100},
                    {id: 6, name:'operation_6', price:200},
                    {id: 7, name:'operation_7', price:300},
                    {id: 8, name:'operation_8', price:400}
                ]
            }
        ]
    },
    {
        id: 2,
        name: 'bank_2',
        accounts: [
            {
                id: 3,
                name: 'account_3',
                operations: [
                    {id: 9, name:'operation_9', price:100},
                    {id: 10, name:'operation_10', price:200},
                    {id: 11, name:'operation_11', price:300},
                    {id: 12, name:'operation_12', price:400}
                ]
            },
            {
                id: 4,
                name: 'account_4',
                operations: [
                    {id: 13, name:'operation_13', price:100},
                    {id: 14, name:'operation_14', price:200},
                    {id: 15, name:'operation_15', price:300},
                    {id: 16, name:'operation_16', price:400}
                ]
            }
        ]
    }
];


为了用Angular正确显示,我想做这样的事情:

<table>
    <tr>
        <th>Banque</th>
        <th>Account</th>
        <th>ID OP</th>
        <th>Name</th>
        <th>Price</th>
    </tr>

    <div ng-repeat="bank in banks">
        <div ng-repeat="account in bank.accounts">
            <div ng-repeat="operation in account.operations">
                <tr>
                    <td>{{banque.name}}</td>
                    <td>{{account.name}}</td>
                    <td>{{operation.id}}</td>
                    <td>{{operation.name}}</td>
                    <td>{{operation.price}}</td>
                </tr>
            </div>
        </div>
    </div>

</table>


但是我知道打破表> tr> td是不正确的。

经过一些研究,我认为我可能必须使用ng-repeat-start,ng-repeat-end指令,但是我不知道如何操作,我甚至不确定这是一件好事。

如果您有任何解决方法的想法,我将很高兴听到您的解决方案。

最佳答案

通过创建将返回操作列表的自定义过滤器,可以轻松实现此目的。

过滤

.filter('filterData', function(){
  return function(data, firstParam, secondParam){
    var returnData = [];
    angular.forEach(data, function(value, index){
      angular.forEach(value[firstParam], function(val, ind){
        angular.forEach(val[secondParam], function(v, i){
          returnData.push(v)
        });
      });
    });
    return returnData;
  }
});


标记

<table>
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Price</th>
  </tr>
  <tr ng-repeat="operation in banks| filterData: 'accounts': 'operations'">
    <td>{{operation.id}}</td>
    <td>{{operation.name}}</td>
    <td>{{operation.price}}</td>
  </tr>

</table>


Working Plunkr在这里

希望这可以对您有所帮助,谢谢。

关于angularjs - Angular -表中有多个ng重复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29074747/

10-16 14:47