如何对角度连续的ng-repeat元素进行连续索引

时间:2015-12-15 09:59:20

标签: javascript angularjs indexing

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

<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>

1 个答案:

答案 0 :(得分:1)

虽然很狡猾,但仍然有效。 '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>