我的 json 如下,
{
"natureitems": [
{
"groups": "fruits",
"items": [
{
"name": "apple",
"properties": {
"color": "red",
"shape": "round"
}
},
{
"name": "orange",
"properties": {
"color": "orange",
"shape": "round"
}
}
]
},
{
"groups": "vegitables",
"items": [
{
"name": "carrot",
"properties": {
"color": "red",
"shape": "cone"
}
},
{
"name": "tomoto",
"properties": {
"color": "red",
"shape": "round"
}
}
]
}
]
}
我需要单个li ng-repeat( Angular View )的输出,例如
------------------
fruits(these titles row in different background colors)
------------------
apple - round
orange - round
----------------
vegitables
---------------
carrot - cone
tomoto - round
我尝试了很多 for 循环和 Angular foreach,但我分别获取项目和分组。但之后尝试引入上述结构(title-items-title -items)就很难引入一个数组了。
是否有任何简单的方法可以将这种格式的这些项目放入数组或对象中?这样我就可以轻松绑定(bind)到 ng-repeat!
最佳答案
如果没有绑定(bind)单层,则需要使用嵌套的ng-repeat
:
<ul>
<li ng-repeat="group in object.natureitems">
<span class="group-title">{{group.groups}}</span>
<ul>
<li ng-repeat="item in group.items">{{item.name+" - "+"item.properties.shape"}}</li>
</ul>
</li>
</ul>
这是一个例子plunker
关于javascript - 复杂 JSON 解析问题(angularjs 和 javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29326511/