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..

2
  • showItems is an object not an array, why do you want to iterate on that? Commented Jun 19, 2015 at 18:12
  • Because I need to show on my view this kind of result: 1443564000000 - 1 1444428000000 -3 Commented Jun 19, 2015 at 20:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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? <div ng-repeat="(key, value) in cartItems"> <h1>{{key}} {{value.sessions}}</h1> </div>
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!
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: jsfiddle.net/dph4fg66/16
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: jsbin.com/zuzoxeciba/1/edit?html,js,output
Another approach will be pushing elements to an array: jsfiddle.net/dph4fg66/17 But the counter initialize everytime I click

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.