ng-repeat能否在Angular Js中使用用户定义的$ index增量?

时间:2015-09-14 11:49:41

标签: javascript html angularjs angularjs-ng-repeat angularjs-ng-if

纯HTML:

<div class="table-tr museo_sans500">
    <div class="table-td"><a href="javascript:void(0)">fgfbgfbfg</a></div>
    <div class="table-td"><a href="javascript:void(0)">Wtrtrdgdgf</a></div>
    <div class="table-td"><a href="javascript:void(0)">Wayne Rooney</a</div>
    <div class="table-td"><a href="javascript:void(0)">fghgfhgfhgf</a></div>
</div>
<div class="table-tr museo_sans500">
    <div class="table-td"><a href="javascript:void(0)"hgfhgfhfg</a></div>
    <div class="table-td"><a href="javascript:void(0)">asdasdasdas</a></div>
    <div class="table-td"><a href="javascript:void(0)">testsfsdfs</a></div>
    <div class="table-td"><a href="javascript:void(0)">gfhgfhfghgf</a></div>
</div>

我需要让它变得动态,我正在使用Angular JS和PHP。 我已经获得了角度$scope.teamSquad中的数据并按以下方式实现:

<div class="table-tr museo_sans500" ng-repeat="(key,squad) in teamSquad">
     <div class="table-td"><a href="javascript:void(0)">{{squad.PlayerName}}</a></div>
</div>

但是我在每次更多值之后添加</div><div class="table-tr museo_sans500">时遇到问题。

我已经尝试{{key%4==0 ? '</div><div class="table-tr museo_sans500">' : ''}}但它不起作用。

我也试过但结果是一样的。

1 个答案:

答案 0 :(得分:1)

正如@entre所说,你应该使用嵌套的ng-repeat来完成这个任务

 <div class="table-tr museo_sans500" ng-repeat="squadChunk in teamSquad">
   <div class="table-td" ng-repeat="squad in squadChunk"><a href="javascript:void(0)">{{squad.PlayerName}}</div>
</div>

将数组拆分为块(大小为4)

var _teamSquad = [
{PlayerName: 1},
{PlayerName: 2},
{PlayerName: 3},
{PlayerName: 4},
{PlayerName: 5},
{PlayerName: 6},
{PlayerName: 7},
{PlayerName: 8}
];

$scope.teamSquad = [];

var size = 4;
while (_teamSquad.length > 0)
$scope.teamSquad.push(_teamSquad.splice(0, size));

Complete Solution