138

In addition to rendering the value of the properties in an object, I'd also like to render the property name as a label. Is there a way to do this with ng-repeat? For example:

<ul>
    <li ng-repeat="option in data">{{propertyName}}: {{option}}</li>
</ul>

Which might spit out something like this:

<ul>
    <li>Name: John</li>
    <li>Phone: (123) 456-7890</li>
    <li>Country: England</li>
</ul>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Brian Flanagan
  • 4,612
  • 5
  • 29
  • 38

2 Answers2

342

Try this:

<ul>
    <li ng-repeat="(key,val) in data">{{key}}: {{val}}</li>
</ul>
Andrew Joslin
  • 43,033
  • 21
  • 100
  • 75
  • 30
    @Andy - they aren't so great, actually. They need more simple actual code examples like you have shown here. Thanks for your answer and please keep posting to StackOverflow; your post really helped me understand how to access the AngularJS docs. Best regards and upvote! – noogrub Jul 01 '12 at 10:01
  • so easy but didn't think of it :D tnx! – Guntram Aug 22 '14 at 09:55
  • what to do if i have a function like --- myFunc(key) --- that need the key value this gonne show me the name key in html and not the value – Nejmeddine Jammeli Jun 06 '15 at 14:09
28

The problem with documentation is that it says (key, value) with that space ... it took me some time to figure out that because of that it doesn't work

d-_-b
  • 21,536
  • 40
  • 150
  • 256
pauldcomanici
  • 597
  • 5
  • 15