ng-repeat中序列号的$ index的替代方法

问题描述 投票:4回答:1

我想显示使用ng-repeat填充的表数据的序列号。我所拥有的代码是这样的

<tr ng-repeat="usageRecord in applicationUsageDataForReport">
  <td style="text-align: center"><span>{{$index+1}}</span></td>
  <td style="padding-left: 5px; max-width: 200px; text-align: center; width:100px;">
    <span>{{usageRecord.ibu}}</span>
  </td>
</tr>

这工作正常。序列号按预期进行。但是当我使用ng-repeatng-if中添加条件时,$index值仅用于满足条件的那些记录,这些条件使序列号按非连续顺序排列。我可以使用的替代方案是什么,以便我能以正确的顺序获得序列号(1,2,3 ..)

javascript angularjs angularjs-ng-repeat
1个回答
3
投票

我创建了一个plnkr,它显示了如何使用ng-repeat过滤器方法。 demo

HTML:

<tr ng-repeat="usageRecord in applicationUsageDataForReport | filter:filterFunction">
  <td style="text-align: center"><span>{{$index+1}}</span></td>
  <td style="padding-left: 5px; max-width: 200px; text-align: center; width:100px;"><span>{{usageRecord.ibu}}</span></td>
</tr>

JS:

$scope.filterFunction = function(item){
    /* return true if included false if excluded */
    return true;
};
© www.soinside.com 2019 - 2024. All rights reserved.