2

I have 2 set of arrays.

Ex: $scope.names = ['John', 'Tyler', 'David']; & $scope.ages = ['30', '35', '28'];

The output exactly i want is

John: 30
Tyler: 35
David: 28

I have tried as below,

<div ng-repeat="name in names">
  <p>{{name}}</p>
  <div ng-repeat="age in ages">
   <p>{{age}}</p>
  </div>
</div>

But i am not getting the values properly. As i am new to angularjs, i searched a lot in internet. Any help?

2
  • Wouldn't it be better to have an object person and do person.name and person.age? Commented Mar 28, 2015 at 6:44
  • @Jakub - Yes. But the array, i am getting from webservice. i dont have control on that. Commented Mar 29, 2015 at 7:09

2 Answers 2

4

Use $index:

<div ng-repeat="name in names">
  <p>{{name}}:{{ages[$index]}}</p>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Wow... a line code... i spended more than one hour. Thanks :)
0

If the length of both array will be same then you can make single object of both the arrays and use that object in ng-repeat. Here is the snippet:

$scope.person = {};

angular.forEach($scope.names, function(value, key) {
  $scope.person.name = value;
  $scope.person.ages = $scope.age[key];
}, log);

Hope it'll help you.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.