使用ng-repeat格式化表,2个数组

时间:2015-02-18 18:58:23

标签: angularjs

尝试使用ng-repeat格式化使用两个数组创建的表时遇到问题。我有两个阵列; game.Teams [0] .Scores和game.Teams [1] .Scores,以及下面的html:

<table>
    <tr>
        <th>
            Team 0 Scores
        </th>
        <th>
            Team 1 Scores
        </th>
    </tr>
    <tr ng-repeat="score in game.Teams[0].Scores">
        <td>
            {{score.ExtraPoints}}
        </td>
        <td>
            ????
        </td>
    </tr>
</table>

我想循环遍历两个列表,并使用第二个列表中的项填充第二列。这是用ng-repeat实现的,还是我必须合并两个并循环一个组合列表?

1 个答案:

答案 0 :(得分:1)

假设两支队伍的物品数量相同

<table ng-init="scores = game.Teams[0].Scores.length > game.Teams[1].Scores.length ? game.Teams[0].Scores : game.Teams[1].Scores">
    <tr>
        <th>
            Team 0 Scores
        </th>
        <th>
            Team 1 Scores
        </th>
    </tr>
    <tr ng-repeat="score in scores">
        <td>
            {{game.Teams[0].Scores[$index].ExtraPoints}}
        </td>
        <td>
            {{game.Teams[1].Scores[$index].ExtraPoints}}
        </td>
    </tr>
</table>

注意:您可以在控制器中执行ngInit部分操作,只显示如何在视图中完成所有操作。