Javascript 重复遍历JSON对象中的子对象
Javascript 重复遍历JSON对象中的子对象,javascript,angularjs,json,Javascript,Angularjs,Json,vm.product返回此JSON { "id": 1, "title": "sample string 2", "customers": [ { "id": 1, "customerid": 10 }, { "id": 2, "customerid": 3 } ] } 我想对客户进行ng repeat,但如何进
vm.product
返回此JSON
{
"id": 1,
"title": "sample string 2",
"customers": [
{
"id": 1,
"customerid": 10
},
{
"id": 2,
"customerid": 3
}
]
}
我想对客户进行ng repeat
,但如何进行
我尝试了ng repeat=“ctrl.customers中的客户”
但此返回值未定义
我缺少的正确方法是什么?您必须将对象添加到范围中。在控制器中执行此操作:
$scope.product = {
"id": 1,
"title": "sample string 2",
"customers": [
{
"id": 1,
"customerid": 10
},
{
"id": 2,
"customerid": 3
}
]
};
然后在html中,您可以执行以下操作:
ng-repeat="customer in product.customers"
这或是一些接近的东西应该可以工作,但我没有测试它。我将ng repeat=“ctrl.customers中的customer
更改为ng repeat=“ctrl.product.customers中的customer”
,并从控制器中删除了vm.customers=vm.product.customers
,然后它就工作了。但我不太清楚为什么另一个不能工作
谢谢大家的评论,让我找到了答案:-)问题不在这段代码中。你是说$scope.vm.customers=$scope.vm.product.customers,对吗?这是一个正在工作的plnkr。你的objectproblem中缺少一个“]”。问题在你的JSON文件中。你得到了……你能找到正确的JSON吗?可能就是正确的JSON文件您应该得到的是{“id”:1,“title”:“示例字符串2”,“customers”:[{“id”:1,“customerid”:10},{“id”:2,“customerid”:3}]}
ng-repeat="customer in product.customers"