在嵌套JSON上使用ng repeat进行迭代
如果我的JSON看起来像在嵌套JSON上使用ng repeat进行迭代,json,angularjs,angularjs-ng-repeat,Json,Angularjs,Angularjs Ng Repeat,如果我的JSON看起来像 [ { "id": "1", "shop_name": "Shop Name", "user_id": "2", "shop_email": "", "shop_address": "", "shop_phone": "", "company_name": "", "description": "", "shop_pro
[
{
"id": "1",
"shop_name": "Shop Name",
"user_id": "2",
"shop_email": "",
"shop_address": "",
"shop_phone": "",
"company_name": "",
"description": "",
"shop_provision": null,
"created_at": "2014-11-05 10:32:32",
"updated_at": "2014-11-06 21:02:00",
"banners": [
{
"id": "18",
"disk_name": "545be1dfa2011452107756.png",
"file_name": "Screenshot from 2014-11-06 09:50:48.png",
"file_size": "135441",
"content_type": "image/png",
"title": null,
"description": null,
"field": "banners",
"sort_order": "18",
"created_at": "2014-11-06 21:02:23",
"updated_at": "2014-11-06 21:02:34",
"path": "/uploads/public/545/be1/dfa/545be1dfa2011452107756.png",
"extension": "png"
},
{
"id": "20",
"disk_name": "545be1e6b5048131391528.png",
"file_name": "Screenshot from 2014-11-06 09:50:48.png",
"file_size": "135441",
"content_type": "image/png",
"title": null,
"description": null,
"field": "banners",
"sort_order": "20",
"created_at": "2014-11-06 21:02:30",
"updated_at": "2014-11-06 21:02:34",
"path": "/uploads/public/545/be1/e6b/545be1e6b5048131391528.png",
"extension": "png"
}
]
}
]
在横幅上,我想再次重复
在控制器上,我得到一个JSON对象或数组,其中包含一个子对象(responseJSON)
子对象
responseJSON看起来像firebug控制台中的结果
Object { result="[{"id":"1","shop_name":"...","extension":"png"}]}]"}
从所有这些我想要的结果=
然后我试着像这样迭代作用域
<div ng-repeat="banner in banners.responseJSON.result track by $index"></div>
而不是再次尝试迭代
{% verbatim %}
{{response}} //outputs
[
{
"id": "2",
"shop_name": "Test Shop",
"user_id": "1",
"shop_email": null,
"shop_address": null,
"shop_phone": null,
"company_name": null,
"description": "",
"shop_provision": null,
"created_at": "2014-11-05 11:31:15",
"updated_at": "2014-11-05 11:31:15",
"banners": []
}
]
<div ng-repeat="shop in response track by $index">
{{ shop.shop_name }} //no data
</div>
{% endverbatim %}
明白了,我永远不会忘记这痛苦
angular.fromJson(jsondata)
你试过这样的东西吗 HTML
<div ng-repeat="shop in responseJson">
{{shop.id}}
<div ng-repeat="banner in shop.banners">
<img ng-src="{{banner.path}}>
</div>
</div>
{{shop.id}
请参见上的,这样您就有了一个控制器,可以设置
$scope.responseJSON=json代码>?你能给我们看一下控制器吗?是的,范围看起来是这样的对不起,我必须纠正我自己,我将更新postis$request
异步还是不异步?此外,banners变量似乎存储商店而不是横幅,变量命名对于可读性很重要yyes$请求是异步的。是一个ajax函数,它在iterate上检索远程数据,我将更改变量名。我从控制器绑定到直接启动范围,但不知何故无法工作
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: shop in response, Duplicate key: string:", Duplicate value: "\""
angular.fromJson(jsondata)
<div ng-repeat="shop in responseJson">
{{shop.id}}
<div ng-repeat="banner in shop.banners">
<img ng-src="{{banner.path}}>
</div>
</div>