【问题标题】:Issue on complex JSON parsing (angularjs and javascript)复杂 JSON 解析的问题(angularjs 和 javascript)
【发布时间】:2015-06-02 07:20:14
【问题描述】:

我有如下的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(角度视图)这样的输出

------------------
fruits(these titles row in different background colors)
------------------
apple   - round
orange  - round

----------------
vegitables
---------------
carrot - cone
tomoto - round

我尝试了很多 for 循环和 angular foreach,但我分别获取项目和组。但是在那之后试图把上面的结构(title-items-title-items)引入一个数组就很难了。

是否有任何简单的方法可以将这些格式的项目放入数组或对象中?这样我就可以轻松绑定到 ng-repeat!

【问题讨论】:

  • 为什么要单级列表?
  • 好的.. 不需要是单级列表。输出需要格式足够好。

标签: javascript json angularjs


【解决方案1】:

如果不绑定单层,需要使用嵌套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 来带来这个输出.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
相关资源
最近更新 更多