710

In my controller, I have data like: $scope.object = data

Now this data is the dictionary with keys and values from json.

I can access the attribute with object.name in the template. Is there any way that I can iterate over the keys as well and display them in table like

<tr><td> {{key}} </td> <td> data.key </td>

The data is like this

{
    "id": 2,
    "project": "wewe2012",
    "date": "2013-02-26",
    "description": "ewew",
    "eet_no": "ewew",
}
Flip
  • 6,233
  • 7
  • 46
  • 75
user192362127
  • 11,385
  • 8
  • 24
  • 21

8 Answers8

1451

How about:

<table>
  <tr ng-repeat="(key, value) in data">
    <td> {{key}} </td> <td> {{ value }} </td>
  </tr>
</table>

This method is listed in the docs: https://docs.angularjs.org/api/ng/directive/ngRepeat

huan feng
  • 7,307
  • 2
  • 32
  • 56
Josh David Miller
  • 120,525
  • 16
  • 127
  • 95
  • 1
    I can't check why your code doesn't work if you don't give me your code. :-) But here is the same thing, modified for ``: http://plnkr.co/edit/7AQF6k7hf2aZbWFmhVoX?p=preview – Josh David Miller Feb 28 '13 at 05:40
  • thanks , thats working now, i think i didn't had proper table markupt with tbody , thats why it wasn't working – user192362127 Feb 28 '13 at 05:43
  • It'll work without that too: http://plnkr.co/edit/7AQF6k7hf2aZbWFmhVoX?p=preview. It was probably just a typo somewhere. Glad I could help! – Josh David Miller Feb 28 '13 at 05:51
  • Yep, I had an extra `tr` in there. I removed it. Sorry about that. – Josh David Miller Feb 28 '13 at 06:03
  • 2
    It works like a charm. The only catch is that it will be alphabetized by the keys so the naming matters if the item order is relevant to the display. – display name Mar 11 '14 at 18:49
  • 31
    @IsabelHM For many reasons, a lot of us recommend against iterating over objects in an `ngRepeat`. In fact, I once heard a core team member regret ever implementing the ability to do so! It's usually better to transform the object in the controller to an array; this makes the intent clearer and decreases the risk for strange/unpredictable behavior in certain cases. And you can sort in the usual way. :-) – Josh David Miller Mar 11 '14 at 19:02
  • 2
    As IsabelHM said, the output is ordered alphabetically by the name. Is there a way to force it not to do so? – newman Sep 06 '14 at 03:29
  • @miliu As far as I am aware, there is no built-in way to do that, no. – Josh David Miller Sep 12 '14 at 06:14
  • Is there any way to pass the key and value to an `ng-include` or to a directive? – CodyBugstein Nov 06 '14 at 15:55
  • @lmray I'm not sure what you mean; isn't that what's happening above? – Josh David Miller Nov 06 '14 at 16:21
  • @JoshDavidMiller Yes, using `ngRepeat` to iterate over an object is very "weird", yet it does have its uses. I have found it to be quite useful... once. – WebWanderer Mar 03 '15 at 21:29
  • 1
    @JoshDavidMiller - As of angular 1.4, ng-repeat over an object no longer plays nicely with orderBy... here's your plunk slightly modified: http://plnkr.co/edit/gi2DQaSIuWn9j3loGG7Q?p=preview. – Seth Flowers Aug 12 '15 at 17:52
  • 4
    @sethflowers As I mentioned in a previous comment, I don't recommend iterating over keys in objects. It would be better to convert it to an array in your controller. Assuming there isn't idiomatic way to do this based on your business model, ES6 makes it very easy: `Object.getOwnPropertyNames(data).map(k => ({key:k, value:data[k]));`. – Josh David Miller Aug 12 '15 at 19:18
  • 1
    @JoshDavidMiller Can you explain your reasoning as to why it's bad to iterate over keys in objects and to use arrays instead? What if you're dealing with large data? I feel like you wouldn't want to have to sacrifice performance by having to search the array for the key every time you add an item to the map. I guess you could get around the performance loss by maintaining a map of key => arrayIndex, but it's adding another layer of complexity and an extra step in retrieval. – Chris Manning Feb 26 '16 at 18:52
  • @ChrisManning I wouldn't say it's "bad", but I don't usually recommend it. Order can be unpredictable, there's often a challenge with inherited proprerties, and objects are not intended to be iterable (so your model probably doesn't reflect your business). But there are always exceptions. – Josh David Miller Feb 26 '16 at 21:32
  • @JoshDavidMiller interesting that you mentioned that because I was ran into one such problem you mentioned after I typed that. Essentially I had to group certain entities based on a group UUID, and then I realized they had a property by which I needed to order them. Guess I need to sort the list pre-map creation. Of course, I had to transform it into a 2d array anyway because I'm using angular 1.2.x Thanks for the explanation/followup. Very much appreciated – Chris Manning Feb 29 '16 at 03:06
  • The URL should be updated to: https://docs.angularjs.org/api/ng/directive/ngRepeat – huan feng Mar 09 '18 at 05:42