1

I am new to AngularJS and I have to say I am finding it exceedingly difficult and fiddly.

I have a json response from a webserver. Typical array of dictionaries. I want to display the name key for each dictionary in the values array. This works:

<ul>
  <li ng-model="projects" ng-repeat="values in projects">
    project name:{{projects.values[$index].name}}
  </li>
</ul>

This does NOT work, but I don't understand why it doesn't:

<ul>
  <li ng-model="projects" ng-repeat="values in projects">
    project name:{{values.name}}
  </li>
</ul>

Plain old {{name}} doesn't work either. None of the examples I've found resort to calling out the array index explicitly with $index, so I must be doing something wrong. Please enlighten me!

2
  • 2
    can you share project json? Commented Mar 20, 2014 at 19:01
  • $index is the index of the <li> list item, which (especially if you apply a filter) will not always correspond to the index of the object you are iterating, Commented Mar 20, 2014 at 19:10

2 Answers 2

3

you don't need ng-model on your list anywhere.

here is a working example of what you're trying to do:

http://jsfiddle.net/8r2zA/1/

and as the others said, if you need access to the index in your iteration you can do:

<ul>
  <li ng-repeat="(index, values) in projects">
    {{projects[index].name}}
  </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer got me the closest: <ul ng-repeat="values in projects.values"> <li>{{values.name}}</li> </ul> Seems weird that you have to repeat values like that. I thought there was some sort of implicit dictionary being passed to the brackets.
think of it as a forEach loop. You're basically saying "repeat each VALUE IN the PROJECTS.VALUES array", so although it doesn't actually matter what you call it; it would make more sense for you to call it value (not values). e.g. ng-repeat="value in projects.values". You could call it bacon in projects.values if you wanted to and reference everything in that array using {{bacon.whateverProperty}}
i'm totally going to call it bacon from now on.
-1

It would be good, to see yours projects structure.
But, you could also try:

<li ng-repeat="(index, name) in projects">
  {{name[index]}}
</li>

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.