简体   繁体   English

AngularJS-使用ng-repeat打印JSON

[英]AngularJS - Print JSON with ng-repeat

New to angularjs and trying to figure out how to print using ng-repeat. angularjs的新手,试图弄清楚如何使用ng-repeat进行打印。

My JSON feed looks like this: 我的JSON供稿看起来像这样:

[
{
    "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. 哪个显示正确的信息-但是我无法使用metric.event.name或metric.meal.name,因此我什么也没打印。

I checked out my JSON with JSONLint and it seems to validate. 我用JSONLint签出了JSON,它似乎可以验证。

Any help is much appreciated. 任何帮助深表感谢。

I'll provide you the two solutions. 我将为您提供两种解决方案。 One that uses array indices and the other that uses all nested ng-repeats: 一个使用数组索引,另一个使用所有嵌套的ng-repeats:

Based on your json you'll probably want to do something like this with multiple repeats: 根据您的json,您可能需要多次重复执行以下操作:

http://plnkr.co/edit/0ocF9clnDmbGAw4kwt8T?p=preview 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 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>

If your json "metric" is an array so you need to iterate on each "metric" too. 如果您的json“指标”是一个数组,那么您也需要对每个“指标”进行迭代。 If "metric" is not an array replace the '[' with '{' when defining it in your json. 如果“ metric”不是数组,则在json中定义时,将“ [[”替换为“ {”。

根据您的JSON结构,我认为您应该使用metric[1].meal[0].name检索您的姓名。

your JSON is valid, but confusing. 您的JSON有效,但令人困惑。 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: 您需要使用HTML来设置转发器中的绑定格式:

<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 或者,您可以使用ng-model将其绑定到元素,以防日后能够直接对其进行修改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM