1

New to angularjs and trying to figure out how to print using ng-repeat.

My JSON feed looks like this:

[
{
    "metric": [
        {
            "event": [
                {
                    "id": 1,
                    "name": "Wedding",
                    "date": "2013-12-17",
                    "time": "2000-01-01T20:47:46Z",
                    "description": "Wedding Desc",
                    "address": "Wedding Address",
                }
            ]
        },
        {
            "meal": [
                {
                    "id": 1,
                    "name": "Chicken",
                    "description": "Chicken Yum!",
                }
            ]
        }
    ]
},
{
    "metric": [
        {
            "event": [
                {
                    "id": 2,
                    "name": "Rehersal",
                    "date": "2013-12-17",
                    "time": "2000-01-01T20:47:46Z",
                    "description": "Rehersal Desc",
                    "address": "Rehersal Address"
                }
            ]
        },
        {
            "meal": [
                {
                    "id": 2,
                    "name": "Steak",
                    "description": "9oz"
                }
            ]
        }
    ]
}
]

And for each "metric" I would like to print it out like this

Event Name:
Date:
Time:
Address:
Event Description:

Meal Name:
Meal Description:

On my template I have:

<div ng-repeat="metric in eventmetrics">
 {{ metric }}
</div>

This prints:

{"metric":[{"event":[{"id":1,"name":"Wedding","date":"2013-12-17","time":"2000-01-01T20:47:46Z","description":"Wedding Desc","address":"Wedding Address"}]},{"meal":[{"id":1,"name":"Chicken","description":"Chicken Yum!"}]}]} 

{"metric":[{"event":[{"id":2,"name":"Rehersal","date":"2013-12-17","time":"2000-01-01T20:47:46Z","description":"Rehersal Desc","address":"Rehersal Address"}]},{"meal":[{"id":2,"name":"Steak","description":"9oz"}]}]}

Which shows the right info - however I can't go metric.event.name or metric.meal.name and I get nothing printed.

I checked out my JSON with JSONLint and it seems to validate.

Any help is much appreciated.

2
  • Could you put a plunkr together for this? Commented Jan 8, 2014 at 20:04
  • I've posted an answer with two solutions along with plunkers. Commented Jan 8, 2014 at 22:57

4 Answers 4

4

I'll provide you the two solutions. One that uses array indices and the other that uses all nested ng-repeats:

Based on your json you'll probably want to do something like this with multiple repeats:

http://plnkr.co/edit/0ocF9clnDmbGAw4kwt8T?p=preview

<div ng-repeat="item in eventmetrics">
 <div ng-repeat="metric in item.metric">
   <div ng-repeat="event in metric.event">

     Event Name: {{event.name}} <br/>
     Date: {{event.date}} <br/>
     Time: {{event.time}} <br/>
     Address {{event.address}} <br/>
     Event Description: {{event.description}} <br />

   </div>

   <div ng-repeat="meal in metric.meal">

   Meal Name: {{meal.name}} <br />
   Meal Description: {{meal.description}} <br /> 
   </div>
  </div>
</div>

which will print out the following:

Event Name: Wedding 
Date: 2013-12-17 
Time: 2000-01-01T20:47:46Z 
Address Wedding Address 
Event Description: Wedding Desc 
Meal Name: Chicken 
Meal Description: Chicken Yum! 
Event Name: Rehersal 
Date: 2013-12-17 
Time: 2000-01-01T20:47:46Z 
Address Rehersal Address 
Event Description: Rehersal Desc 
Meal Name: Steak 
Meal Description: 9oz 

If you want to use the array approach which produces the same textual output (one less div though):

http://plnkr.co/edit/jUA22TJHfu0lZC1rgjNk?p=preview

<div ng-repeat="item in eventmetrics">

   <div ng-repeat="event in item.metric[0].event">

     Event Name: {{event.name}} <br/>
     Date: {{event.date}} <br/>
     Time: {{event.time}} <br/>
     Address {{event.address}} <br/>
     Event Description: {{event.description}} <br />

   </div>

   <div ng-repeat="meal in item.metric[1].meal">

   Meal Name: {{meal.name}} <br />
   Meal Description: {{meal.description}} <br /> 
   </div>
  </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

If your json "metric" is an array so you need to iterate on each "metric" too. If "metric" is not an array replace the '[' with '{' when defining it in your json.

2 Comments

Thanks - basically a metric has a array of event info and meal info. I went ahead and did this: <div ng-repeat="metric in eventmetrics"> <div ng-repeat="event in metric"> {{ event.name }} </div> </div> And I still don't get any data!
@OwenHope event is also an array, you should be able to reference event[0].name
0

According to your JSON structure, I think you should use metric[1].meal[0].name to retrieve your name.

3 Comments

It should be metric[0]. Event is the first element of the metric array.
@SimonBelanger you are right, but meal is in #2 of the array.
Correct. I was under the impression meal was within the event array.
0

your JSON is valid, but confusing. Your metrics have document arrays inside them, so unless they're always ordered the same, you're going to have to repeat through them as well.

further, your event and meals are arrays seemingly unnecessarily. I'd look into the composure of them.

you need to use HTML to format your bindings inside the repeater:

<div ng-repeat="metric in eventmetrics">
 Event Name: {{metric[0].event[0].name}} <br/>
 Date: {{metric[0].event[0].date}} <br/>
 Time: {{metric[0].event[0].time}} <br/>
 Address {{metric[0].event[0].address}} <br/>
 Event Description: {{metric[0].event[0].description}} <br />
 <br />
 Meal Name: {{metric[1].meal[0].name}} <br />
 Meal Description: {{metric[1].meal[0].description}} <br />
</div>

or you could bind it to an element using ng-model in case you want to be able to directly modify it later

name: <input ng-model="metric[0].event[0].name"></input>

3 Comments

Hmm I am still not getting any data printed, just the labels.
I just edited to take into account that metric is also an array
Yeah, with the updated code I am still seeing no data, even though it is looping the correct amount of times.

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.