在ng-repeat中显示嵌套的json

时间:2015-02-09 15:14:06

标签: javascript json angularjs

我似乎很难掌握如何向下钻取以获得嵌套的JSON并使用angular在页面上显示它。例如,我有以下JSON结构,我想在ng-repeat中显示投资组合下的连接产品...

{
"addons": [
    ...
],

"attributes": [     
    ...
],
"portfolios": [
    {
        "connectivity": [
            {
                "product-1": {
                    "label": "product-1",
                    "description": "Description in here"
                }
            },
            {
                "product-2": {
                    "label": "product-2",
                    "description": "Description in here"
                }
            }
        ]

    }
]

}

到目前为止,我已经尝试过两种不同的方法。

$scope.listOfProducts = allProducts.data.portfolios.connectivity;

和ng-repeat

ng-repeat='product in listOfProducts.portfolios.connectivity'

循环显示'连接的正确方法是什么? ng-repeat中的产品?感谢

修改

我已将JSON更改为此结构...

{
"addons": [
...
],

"attributes": [     
    ...
],
"portfolios": [
{
    "connectivity": [
        {
                "label": "product-1",
                "description": "Description in here"
        },
        {
                "label": "product-2",
                "description": "Description in here"
        }
    ]

}
]

但我仍然无法通过ng-repeat来显示连接中的产品。

$ scope.listOfProducts = allProducts.data.portfolios.connectivity

1 个答案:

答案 0 :(得分:2)

由于listOfProducts已设置为connectivity数组,因此您只需ng-repeat="product in listOfProducts"

<div ng-repeat="product in listOfProducts">
  {{product.label}}
</div>

编辑:嗯,您的数组有点不规则,因为您为每个项目创建了一个名为product-[index]的属性。您是否可以控制返回的数据?你的数组应该只有对象,如:

"connectivity": [
    {
        "label": "product-1",
        "description": "Description in here"
    },
    {
        "label": "product-2",
        "description": "Description in here"
    }
]