如果只有一个元素是AngularJS NG重复不起作用 [英] AngularJS ng-repeat doesn't work if there is only one element
问题描述
下面是HTML code:
< DIV ID =通知 - 饲料级=内容的数据-NG-控制器=feedContentCtrl>
< DIV数据-NG-重复=条目cmsData.rss.channel.item> < DIV数据-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>中
},
}}
如果有一个以上的项的元素也能正常工作。然而,在这种情况下,只有一个,这样什么也不显示。如果我只是把:
< DIV数据-NG-绑定=cmsData.rss.channel.item.desciption>< / DIV>
然后,它显示了。难道只有一个,当NG-重复不行?
修改:通过改变code为固定:
< DIV数据-NG-重复=(项目,项目)的cmsData.rss.channel> < DIV数据-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='/news'>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屋!