如果只有一个元素是AngularJS NG重复不起作用 [英] AngularJS ng-repeat doesn't work if there is only one element

查看:113
本文介绍了如果只有一个元素是AngularJS NG重复不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是HTML code:

 < D​​IV ID =通知 - 饲料级=内容的数据-NG-控制器=feedContentCtrl>
    < D​​IV数据-NG-重复=条目cmsData.rss.channel.item>            < D​​IV数据-NG绑定-HTML =entry.description>< / DIV>
    < / DIV>
< / DIV>

下面是获取从URL读取JSON:

  {
RSS:
{
    渠道:
    {
        说明:,
        项目:
        {
            说明:等等等等等等,
            链接:http://foo.bar
            pubdate的:星期五,2014年2月21日 - 17:52,
            头衔:服务器维护
        },
        语言:恩,
        链接:http://foo.bar/alerts
        头衔:< A HREF =&放大器;#039; /新闻与#039;>新闻与更新与LT; \\ / A>中
    },
}}

如果有一个以上的项的元素也能正常工作。然而,在这种情况下,只有一个,这样什么也不显示。如果我只是把:

 < D​​IV数据-NG-绑定=cmsData.rss.channel.item.desciption>< / DIV>

然后,它显示了。难道只有一个,当NG-重复不行?

修改:通过改变code为固定:

 < D​​IV数据-NG-重复=(项目,项目)的cmsData.rss.channel>    < D​​IV数据-NG绑定-HTML =item.description>< / DIV>
< / DIV>


解决方案

由于在评论中说, NG-重复你的方法是一个数组。使用的对象和属性的语法如下:

 (K,V)在cmsData.rss.channel.item

这将迭代里面 cmsData.rss.channel.item

每个键/值对

Here is the html code:

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

{
"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:

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

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

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>

解决方案

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

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

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

这篇关于如果只有一个元素是AngularJS NG重复不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆