0

One controller returns me a very complex json object:

{
"184": {
    "title": "THE STONE ROSES",
    "sessions": {
        "1443564000000": {
            "num": 5
        },
        "1442959200000": {
            "num": 1
        },
        "1447196400000": {
            "num": 1
        },
        "1444428000000": {
            "num": 2
        },
        "1446332400000": {
            "num": 3
        }
    }
}

}

I have tried to iterate over it on my view like this

<div class="large-6 columns" ng-repeat="item in showItems" st-raw>
    <h1>{{item.sessions}}</h1>
 </div> 

with this code I get part if the object printed on the html response:

{"1443564000000":{"num":1}}

But as you can see, sessions has a very complex attribute (I use the id of the session to store the number of it)

I have seen that some people used to do that with ng-repeat-start, on my case using it gives me severals errors..

Frnnd Sgz
  • 236
  • 1
  • 4
  • 19

1 Answers1

4

Assuming that you want to iterate over the sessions property, which is not an array but an object, you would have to iterate over the object's properties:

<tr ng-repeat="(key, value) in data">

Here is an example: http://plnkr.co/edit/7AQF6k7hf2aZbWFmhVoX?p=preview

relevant SO questions where the example is from: How can I iterate over the keys, value in ng-repeat in angular

Community
  • 1
  • 1
timeiscoffee
  • 468
  • 3
  • 14
  • Yeps! this is almost working...except for that I'm getting the whole session object if I iterate like this...is there a way to stringfy this object? `

    {{key}} {{value.sessions}}

    `
    – Frnnd Sgz Jun 19 '15 at 18:38
  • 1
    Sorry, I do not understand your question. Are you trying to iterate over cartItem _and_ session? If you could post a code example (jsbin) it would be helpful! – timeiscoffee Jun 19 '15 at 20:51
  • I will try to explain, I will reproduce the code on fiddle but without the real data just to see what I'm doing. What I'm try to do is a kind of shop cart: everytime you click on date session of an item you have to add to the cart the session time and the number of times you select this date session. What I have done is create an especific object for the event and then I push diferents attributes on it for store the sessions: http://jsfiddle.net/dph4fg66/16/ – Frnnd Sgz Jun 19 '15 at 21:05
  • you can easily mock data in angular using $q or just forgoing the async call. I've created a template for a simple mocking; try to recreate your issue here: http://jsbin.com/zuzoxeciba/1/edit?html,js,output – timeiscoffee Jun 19 '15 at 21:11
  • Another approach will be pushing elements to an array: http://jsfiddle.net/dph4fg66/17/ But the counter initialize everytime I click – Frnnd Sgz Jun 19 '15 at 21:13