确定 ng-repeat 元素的索引
Determine index of ng-repeat element
我想知道是否有一种方法可以在处理 ng-repeat 时确定索引。
比如我只想让第一个和第二个索引在下面有锚标签:
<tbody>
<tr ng-repeat="row in rows | orderBy:sort.type:sort.reverse | filter:searchData">
<!-- only want to make the index 0 and 1 have an anchor -->
<td ng-repeat="column in cols"><a href="#">{{row[column]}}</a></td>
</tr>
</tbody>
我认为需要像下面这样的东西,但我想不出(或找到)正确的语法:
<!-- if index == 0 or 1, show anchor -->
<td ng-repeat="column in cols" ng-if="row[$index] <2"><a href="#">{{row[column]}}</a></td>
<!-- else, if index >= 2, don't show anchor -->
<td ng-repeat="column in cols" ng-if="row[$index] >=2">{{row[column]}}</td>
这是一个带有基础示例的 fiddle:https://jsfiddle.net/zp2cqxqb/
感谢您对此的任何帮助!
您对 $index 本身感兴趣,而不是您当前正在检查的行[$index] 值。
作为奖励,如果您使用另一个内联元素(例如 span)作为非锚值,则可以避免双重 ng-repeat 语句:
<tr ng-repeat="row in rows | orderBy:sort.type:sort.reverse | filter:searchData">
<!-- only want to make the index 0 and 1 have an anchor -->
<td ng-repeat="column in cols">
<a ng-if="$index < 2" href="#">{{row[column]}}</a>
<span ng-if="$index >= 2">{{row[column]}}</span>
</td>
</tr>
已更新 fiddle:https://jsfiddle.net/z87ntjne/
我想知道是否有一种方法可以在处理 ng-repeat 时确定索引。
比如我只想让第一个和第二个索引在下面有锚标签:
<tbody>
<tr ng-repeat="row in rows | orderBy:sort.type:sort.reverse | filter:searchData">
<!-- only want to make the index 0 and 1 have an anchor -->
<td ng-repeat="column in cols"><a href="#">{{row[column]}}</a></td>
</tr>
</tbody>
我认为需要像下面这样的东西,但我想不出(或找到)正确的语法:
<!-- if index == 0 or 1, show anchor -->
<td ng-repeat="column in cols" ng-if="row[$index] <2"><a href="#">{{row[column]}}</a></td>
<!-- else, if index >= 2, don't show anchor -->
<td ng-repeat="column in cols" ng-if="row[$index] >=2">{{row[column]}}</td>
这是一个带有基础示例的 fiddle:https://jsfiddle.net/zp2cqxqb/
感谢您对此的任何帮助!
您对 $index 本身感兴趣,而不是您当前正在检查的行[$index] 值。
作为奖励,如果您使用另一个内联元素(例如 span)作为非锚值,则可以避免双重 ng-repeat 语句:
<tr ng-repeat="row in rows | orderBy:sort.type:sort.reverse | filter:searchData">
<!-- only want to make the index 0 and 1 have an anchor -->
<td ng-repeat="column in cols">
<a ng-if="$index < 2" href="#">{{row[column]}}</a>
<span ng-if="$index >= 2">{{row[column]}}</span>
</td>
</tr>
已更新 fiddle:https://jsfiddle.net/z87ntjne/