user2851669 user2851669 - 4 months ago 60
AngularJS Question

looping multiple arrays simultaneously with ng-repeat

Can I traverse two arrays simultaneously using only one

ng-repeat
in AngularJs? If so, then how?

For example I have two arrays

array1 = [1,2,3,4,5]

array2 = [6,7,8,9,10]


It should be able to produce the same index for both the arrays.

Answer

If you want to acces the second array with the index of the first, try this:

$scope.arr1 = [1, 2, 3, 4, 5]
$scope.arr2 = [6, 7, 8, 9, 10]

<div ng-repeat="number in arr1">
    Number from array1 = {{number}}
    Number from array2 = {{arr2[$index]}}
</div>

See this fiddle: http://jsfiddle.net/dm9zhgx9/