gpt4 book ai didi

javascript - 查找 ng-repeat 索引?

转载 作者:行者123 更新时间:2023-11-28 01:46:04 25 4
gpt4 key购买 nike

我可以在 Angular.js 中做到这一点:

<tr ng-repeat="cust in customers">
<td>
{{ cust.name }}
</td>
</tr>

这将通过将每个客户放入 <tr> 来遍历所有客户自己的。

但是如果我想要两个客户合二为一怎么办 <tr> ?我将如何实现?

我通常通过弄乱索引和模数值来做到这一点,但我不确定如何在这里做到这一点。

最佳答案

事实证明,这可以在没有任何自定义过滤器或更改数据格式的情况下完成,尽管它确实需要一些额外的标记。我认为一开始这是不可能的,因为您不能在表中为您的 ng-repeat 使用 span 或任何类似的东西。 This answer但是,指出您可以使用 tbody

所以这只是使用 ng-if 指令(如果表达式为真则呈现 html),$index 变量(由 提供) ng-repeat) 和 $even 变量(也由 ng-repeat 提供,当 $index 是甚至

我在 this Plunker 中创建了一个演示

<div  ng-controller="MainCtrl">
<table>
<tbody ng-repeat="cust in customers">
<tr ng-if="$even">
<td>{{customers[$index].text}}</td>
<td>{{customers[$index+1].text}}</td>
</tr>
</tbody>
</table>
</div>

这当然只有在你有两列时才有效,如果你有更多呢?你也可以将完整的表达式放入 ng-if 而不仅仅是一个变量。所以你可以像这样使用模值:

    <tbody ng-repeat="cust in customers">
<tr ng-if="($index % 3) == 0">
<td>{{customers[$index].text}}</td>
<td>{{customers[$index+1].text}}</td>
<td>{{customers[$index+2].text}}</td>
</tr>
</tbody>

关于javascript - 查找 ng-repeat 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com