【问题标题】:How to give continuous indexing for consecutive ng-repeat elements in angular如何以角度为连续的 ng-repeat 元素提供连续索引
【发布时间】:2016-03-21 01:22:50
【问题描述】:

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

【问题讨论】:

  • 为什么不连接你的数组并且只做一个 ng-repeat ?
  • 或使用 HTML 的有序列表
  • @Alexandre Divs 在不同的地方。并添加到不同事件的页面中。
  • @FreddyPv 好的,也许你可以使用每个数组的长度?
    {{$index+1}}_{{item}}
    {{array1.length+$index+ 1}}_{{item}}
  • @binariedMe 不能使用有序列表。其实div里面的内容比较多

标签: javascript angularjs indexing


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多