如果只有一个元素,AngularJS ng-repeat不起作用

时间:2022-08-23 12:37:24

Here is the html code:

这是html代码:

<div id="notifications-feed" class="content" data-ng-controller="feedContentCtrl">
    <div data-ng-repeat="entry in cmsData.rss.channel.item">

            <div data-ng-bind-html="entry.description"></div>
    </div>
</div>

Here is the JSON that gets read from a URL:

以下是从URL读取的JSON:

{
"rss": 
{
    "channel": 
    {
        "description": "",
        "item": 
        {
            "description": "blah blah blah",
            "link": "http://foo.bar",
            "pubDate": "Friday, February 21, 2014 - 17:52",
            "title": "Server Maintenance"
        },
        "language": "en",
        "link": "http://foo.bar/alerts",
        "title": "<a href=&#039;/news&#039;>News & Updates<\/a>"
    },
}} 

This works fine if there are more than one "item" elements. However, in this case there is only one so nothing shows up. If I just put:

如果有多个“item”元素,这可以正常工作。但是,在这种情况下,只有一个没有显示出来。如果我只是说:

<div data-ng-bind="cmsData.rss.channel.item.desciption"></div>

Then it shows up. Does ng-repeat not work when there is only one?

然后就出现了。当只有一个时,ng-repeat不起作用吗?

Edit: Fixed by changing the code to:

编辑:通过将代码更改为:

<div data-ng-repeat="(items, item) in cmsData.rss.channel">

    <div data-ng-bind-html="item.description"></div>
</div>

3 个解决方案

#1


4  

As said in the comments, ng-repeat the way you have is for an array. Use the following syntax for objects and properties:

如评论中所述,ng-repeat的方式是数组。对对象和属性使用以下语法:

(k, v) in cmsData.rss.channel.item

This will iterate each key/val pair inside cmsData.rss.channel.item

这将迭代cmsData.rss.channel.item中的每个键/值对

#2


1  

The reason is because in the JSON object, the "item", isn't an Array.

原因是因为在JSON对象中,“item”不是Array。

To fix:

修理:

{
"rss": 
{
    "channel": 
    {
        "description": "",
        "item": 
        [{
            "description": "blah blah blah",
            "link": "http://foo.bar",
            "pubDate": "Friday, February 21, 2014 - 17:52",
            "title": "Server Maintenance"
        }],
        "language": "en",
        "link": "http://foo.bar/alerts",
        "title": "<a href=&#039;/news&#039;>News & Updates<\/a>"
    },
}}  

#3


0  

You are referencing an object not an array, check the docs for using an object in ngRepeat's arguments.

您正在引用一个对象而不是一个数组,检查文档是否在ngRepeat的参数中使用了一个对象。

http://docs.angularjs.org/api/ng/directive/ngRepeat

http://docs.angularjs.org/api/ng/directive/ngRepeat

You would need data-ng-repeat="(key, value) in cmsData.rss.channel.item

你需要在cmsData.rss.channel.item中使用data-ng-repeat =“(key,value)

Then you could use <div data-ng-bind-html="value"></div> which would list all of item's properties.

然后你可以使用

来列出所有项目的属性。

#1


4  

As said in the comments, ng-repeat the way you have is for an array. Use the following syntax for objects and properties:

如评论中所述,ng-repeat的方式是数组。对对象和属性使用以下语法:

(k, v) in cmsData.rss.channel.item

This will iterate each key/val pair inside cmsData.rss.channel.item

这将迭代cmsData.rss.channel.item中的每个键/值对

#2


1  

The reason is because in the JSON object, the "item", isn't an Array.

原因是因为在JSON对象中,“item”不是Array。

To fix:

修理:

{
"rss": 
{
    "channel": 
    {
        "description": "",
        "item": 
        [{
            "description": "blah blah blah",
            "link": "http://foo.bar",
            "pubDate": "Friday, February 21, 2014 - 17:52",
            "title": "Server Maintenance"
        }],
        "language": "en",
        "link": "http://foo.bar/alerts",
        "title": "<a href=&#039;/news&#039;>News & Updates<\/a>"
    },
}}  

#3


0  

You are referencing an object not an array, check the docs for using an object in ngRepeat's arguments.

您正在引用一个对象而不是一个数组,检查文档是否在ngRepeat的参数中使用了一个对象。

http://docs.angularjs.org/api/ng/directive/ngRepeat

http://docs.angularjs.org/api/ng/directive/ngRepeat

You would need data-ng-repeat="(key, value) in cmsData.rss.channel.item

你需要在cmsData.rss.channel.item中使用data-ng-repeat =“(key,value)

Then you could use <div data-ng-bind-html="value"></div> which would list all of item's properties.

然后你可以使用

来列出所有项目的属性。