Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I can do a ng-repeat that covers more than one array with AngularJS?

Tags:

angularjs

I have the following that loops through xa and prints the qs.a.text values:

<div data-ng-repeat="a in xa">
   <div data-ng-bind-html="a.text"></div>
</div>

<div data-ng-repeat="b in xb">
   <div data-ng-bind-html="b.text"></div>
</div>

Is there a way that I could combine these something like this:

<div data-ng-repeat="a in xa and xb">
   <div data-ng-bind-html="a.text"></div>
   <div data-ng-bind-html="b.text"></div>
</div>

Note that xa and xb are arrays that always contain exactly the same number of elements.

like image 358
Alan2 Avatar asked Nov 16 '13 17:11

Alan2


3 Answers

Sure, you can use javascript's concat() to concatenate the arrays and then use the results for your repeat.

As such:

<div data-ng-repeat="item in combined = xa.concat(xb)">
   <div data-ng-bind-html="item.text"></div>
</div>
like image 86
KayakDave Avatar answered Oct 21 '22 11:10

KayakDave


you can use $index. it represents the current index of the ng-repeat

Fiddle Here

<div ng-repeat="item in arrA" class="container">
    <div>{{arrA[$index].text}}</div>
    <div>{{arrB[$index].text}}</div>
</div>

Though you may want to combine your arrays in to one with something like undescorejs instead of doing that.

like image 32
Quad Avatar answered Oct 21 '22 10:10

Quad


Try this

<div data-ng-repeat="a in xa">
   <div data-ng-bind-html="a.text"></div>
   <div data-ng-bind-html="xb[$index].text"></div>
</div>
like image 20
dimirc Avatar answered Oct 21 '22 12:10

dimirc