0

I am creating some div elements using multiple ng-repeat. Number of ng-repeat elements are dynamic.I want to give continuous indexing for all elements.How is it done in angular way?

like

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

Expected result

<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>
7
  • 1
    why not concat your arrays and do only one ng-repeat ?
    – Alexandre
    Commented Dec 15, 2015 at 10:01
  • or use ordered list of HTML
    – binariedMe
    Commented Dec 15, 2015 at 10:02
  • @Alexandre Divs are at different places. And added to the pages on different events.
    – Freddy Pv
    Commented Dec 15, 2015 at 10:05
  • 1
    @FreddyPv ok, maybe you can use length of each array ? <div ng-repeat="item in array1">{{$index+1}}_{{item}}</div> <div ng-repeat="item in array2">{{array1.length+$index+1}}_{{item}}</div>
    – Alexandre
    Commented Dec 15, 2015 at 10:06
  • @binariedMe Cannot use ordered list. Actually there are more content in div
    – Freddy Pv
    Commented Dec 15, 2015 at 10:08

1 Answer 1

1

This will work although it is dodgy. 'profile' is used to represent an object to avoid isolated scope. You can write the directive with this logic.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.