Json Angularjs ng重复数组与对象
我使用ng repeat以atom提要的形式显示来自端点的视图中的数据。如果Accept标头为“application/JSON”,则该端点返回JSON,即JSON是由转换器从服务器端的XML创建的,不幸的是,如果atom响应中有一个条目,则JSON中的条目不是数组,并且ng repeat无法按预期工作。我有一个项目,我使用计数器手动处理这个问题,然后根据计数器和ng show,我要么使用ng repeat,要么只显示提要中的单个条目。我如何正确处理这个问题?我应该在JS端修改传入的JSON吗?如果是的话,有人能告诉我正确的方法吗Json Angularjs ng重复数组与对象,json,angularjs,arrays,ng-repeat,jsonobject,Json,Angularjs,Arrays,Ng Repeat,Jsonobject,我使用ng repeat以atom提要的形式显示来自端点的视图中的数据。如果Accept标头为“application/JSON”,则该端点返回JSON,即JSON是由转换器从服务器端的XML创建的,不幸的是,如果atom响应中有一个条目,则JSON中的条目不是数组,并且ng repeat无法按预期工作。我有一个项目,我使用计数器手动处理这个问题,然后根据计数器和ng show,我要么使用ng repeat,要么只显示提要中的单个条目。我如何正确处理这个问题?我应该在JS端修改传入的JSON吗?
<feed xmlns="http://www.w3.org/2005/Atom">
<id>/record</id>
<title type="text">Search feed</title>
<link href="/record" rel="self"/>
<link href="/record?page=2" rel="next"/>
<entry>
<id>recordid</id>
<link href="/record/id/recordid" rel="self"/>
<content type="recordcontenttype">
<record>...recordhere...</record>
</content>
</entry>
</feed>
{
"feed": {
"entry":
{
"content": {
...recordhere...
},
"type": "recordcontenttype"
},
"id": "recordid",
"link": {
"href": "/record/id/recordid",
"rel": "self"
}
},
-- if there would be more entries then entry would be an array [ { xx }, { xx } ] and ng-repeat would work --
"id": "/record",
"link": [
{
"href": "/record",
"rel": "self"
},
{
"href": "/record?page=2",
"rel": "next"
}
],
"title": {
"content": "Search feed",
"type": "text"
}
}
}
/记录
搜索源
记录编号
…记录在这里。。。
{
“提要”:{
“条目”:
{
“内容”:{
…记录在这里。。。
},
“类型”:“recordcontenttype”
},
“id”:“记录id”,
“链接”:{
“href”:“/record/id/recordid”,
“rel”:“self”
}
},
--若有更多的条目,那个么条目将是一个数组[{xx},{xx}],ng repeat将起作用--
“id”:“/记录”,
“链接”:[
{
“href”:“/记录”,
“rel”:“self”
},
{
“href”:“/记录?页面=2”,
“rel”:“下一步”
}
],
“标题”:{
“内容”:“搜索提要”,
“类型”:“文本”
}
}
}
我建议使用一个简单的过滤器,例如:
(功能(应用程序,ng){
"严格使用",;
app.controller('AppCtrl',函数AppCtrl(){
var vm=这个;
vm.foo={id:1};
vm.bar=[{id:2},{id:3}];
});
应用过滤器('ensureArray',函数(){
返回函数(输入){
返回ng.isArray(输入)?输入:[input];
};
})
}(角度模块('app',[]),角度)代码>
{{foo | json}
{{bar | json}}