-1

I got an array with objects similar to:

{
    _id:       "0",
    category: "ptr",
    price:    14.5
}

So I'm printing an HTML table this way:

<tr ng-repeat="product in products.categoryQuery">
    <td>{{product._id}}</td>
    <td>{{product.category}}</td>
    <td>{{product.price}}</td>
</tr>

Being queryCategory an array with my objects. I wonder if I can reduce the HTML and add an ng-repeat directive at a <td> for iterating over object's attributes.

I tried:

<td ng-repeat"p for p in product">{{p}}</td>

and

<td ng-repeat"p for p in product">{{product[p]}}</td>

But got not results. How could I achieve it?

Edit.

As critics suggest, there's this alternative when it's got one single object:

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

But how can I use it when they're several objects from an array?

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • This is NOT a duplicate, in that case I got one single object and here I got an array with objects. – diegoaguilar Jun 07 '14 at 20:41
  • The duplicate shows how to iterate over an object's properties. Whether it's in an array or not makes no difference. – JJJ Jun 07 '14 at 20:43

1 Answers1

-1

Take a look at the responses in this question: How to use ng-repeat for dictionaries in AngularJs?

Essentially you should be able to follow the (k, v) in expression syntax (see https://docs.angularjs.org/api/ng/directive/ngRepeat in the Arguments section)

The one thing I would say is that the order of the keys might not be in the order you expect, which could cause issues: Elements order in a "for (… in …)" loop

Community
  • 1
  • 1
Andrew Burgess
  • 5,300
  • 5
  • 30
  • 37