我想显示使用 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-if
在 ng-repeat
中添加条件时,$index
值仅适用于满足条件的那些记录以非连续顺序制作序列号。我可以使用什么替代方案,以便我可以按正确的顺序获得序列号(1,2,3..)
最佳答案
我创建了一个展示如何使用 ng-repeat 过滤器方法的 plnkr。 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;
};
关于javascript - ng-repeat 中序列号的 $index 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47891842/