我正在使用多个 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/