1

I have an object like this

Obj = {
  "elements":[
               {"name":"something","id":"v1-234"},
               {"name":"somethingElse","id":"v1-239"}
             ],
  "paging":{
    "next" : "100",
    "total": "2000"  
   },
 "linked"={
            "partners":[
                         {"id":"82","name":"A"},
                         {"id":"83","name":"B"}
                       ],
             "instructors":
                       [
                         {"id":"11232","name":"alex"},
                         {"id":"11432","name":"boob"}
                       ]
           }
    }

I have equal items inside the elements array and the partner and instructors array. I have only placed 2 items just for display there could be many.

The Obj.elements[0] is related to Obj.linked.partners[0] and Obj.linked.instructors[0]. Similarly for the first,second,third ..... items as well.

How can I ng-repeat on this object such that I can show

Obj.elements[i] Obj.linked.partners[i] Obj.linked.instructors[i]

at a time in html template ?

1
  • You will have to format your viewmodel to meet your needs. Commented Sep 16, 2016 at 21:21

2 Answers 2

5

If your elements, partners, and instructors arrays will always be the same length and you wanted to for example join together the names you could try something like this.

<div ng-repeat="element in Obj.elements">
    {{element.name}} {{Obj.linked.partners[$index].name}} {{Obj.linked.instructors[$index].name}}
</div>

$index is used by Angular in a ng-repeat to keep track of its position when iterating through an object. Here is the documentation: ng-repeat

Sign up to request clarification or add additional context in comments.

2 Comments

I think Obj.elemnts would throw an error since it doesn't seem a valid repeat_expression (see docs) you should modify it in something like ng-repeat="item in Obj.elements" even if you don't use item
It wouldn't throw an error because Obj.elements is an array.
1

ng-repeat has the $index scoped variable:

<ANY ng-repeat="element in model.elements">
   <span ng-bind="element.name"></span>
   <span ng-bind="model.linked.partners[$index].name"></span>
   <span ng-bind="model.linked.instructors[$index].name"></span>
</ANY>

2 Comments

I think the span should be ng-bind=model.linked.[...]
@drakyoko should be what? ;P

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.