javascript - 如何为 Angular 中的连续 ng-repeat 元素提供连续索引

标签 javascript angularjs indexing

我正在使用多个 ng-repeat 创建一些 div 元素。 ng-repeat元素的数量是动态的。我想为所有元素提供连续索引。如何以 Angular 方式完成?

喜欢

<div ng-repeat="item in [1,2,1,5]">{{$index+1}}_{{item}}</div>
<div ng-repeat="item in [5,2]">{{$index+1}}_{{item}}</div>
<div ng-repeat="item in [4,6]">{{$index+1}}_{{item}}</div>

预期结果

<div>1_1</div> 
<div>2_2</div> 
<div>3_1</div> 
<div>4_5</div> 
<div>5_5</div>
<div>6_2</div>
<div>7_4</div>
<div>8_6</div>

最佳答案

这会起作用,尽管它很狡猾。 “profile”用于表示一个对象以避免孤立的范围。您可以使用此逻辑编写指令。

<span ng-init="profile.count = 0">{{profile.count}}</span>
<div ng-repeat="item in [1,2,1,5] track by $index" 
     ng-init="count=profile.count+1;profile.count=profile.count+1">{{::count}}_{{item}}</div>
<div ng-repeat="item in [5,2] track by $index" 
     ng-init="count=profile.count+1;profile.count=profile.count+1">{{::count}}_{{item}}</div>
<div ng-repeat="item in [4,6] track by $index" 
     ng-init="count=profile.count +1;profile.count=profile.count+1">{{::count}}_{{item}}</div>

关于javascript - 如何为 Angular 中的连续 ng-repeat 元素提供连续索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286071/

相关文章:

mysql - 如何使用 innodb 引擎减少 mysql 中表的索引大小?

algorithm - 计算数字组合的索引

javascript - 我将如何过滤从 Slack 返回的数据以找到活跃用户?

javascript - 计算数组中出现的次数并对它们进行排序

javascript - AngularJS 单元测试 : Injected Services undefined

从 API 下载 AngularJS CSV 文件

linux - 使用 awk 将索引列添加到 csv

javascript - Soundcloud SC.stream() : waveforms and autoplay

javascript - 如何使用随机值维护下拉列表中第二个选项的选定状态?

javascript - 将 ng-model 的一部分与 angularjs 中的范围绑定(bind)