Angular.js - 带Angular的嵌套JSON范围

时间:2014-09-23 13:01:18

标签: json angularjs angularjs-scope angularjs-ng-repeat

{
  "imports": {
    "imported": [
      {
        "date": "19/9/2014",
        "item": [
          {
            "sn": "3366698",
            "type": "Food",
            "weight": "10tn."
          },
          {
            "sn": "3366699",
            "type": "Eqipment",
            "weight": "20kg."
          }
        ]
      },
      {
        "date": "20/9/2014",
        "item": [
          {
            "sn": "3366700",
            "type": "Electronics",
            "weight": "100pt."
          },
          {
            "sn": "3366701",
            "type": "Food",
            "weight": "5tn."
          }
        ]
      }
    ]
  }
}

我有这个json,我不确定它是否在正确的结构中。我试图通过以下$ .getJSON方法将每个项目类型(包括重复项)呈现为表头:

$scope.items = data.imports.item;

和HTML as:

<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>

但我无法成功。我做错了什么?

编辑:jsfiddler

2 个答案:

答案 0 :(得分:0)

我根本没有看到您使用的是进口商品,请尝试

$scope.items = data.imports.imported;

因为导入的是您的数组,而不是项目。

<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>

答案 1 :(得分:0)

你的json坏了,把你的json贴在这里: http://jsonformatter.curiousconcept.com/

您会看到data.imports.item未定义。

正确访问的JSON如下所示:

{
  "imports": {

        "item": [
          {
            "sn": "3366698",
            "type": "Food",
            "weight": "10tn."
          },
          {
            "sn": "3366699",
            "type": "Eqipment",
            "weight": "20kg."
          }
        ]
      }
}

在以下情况之后访问您的数据:

<th ng-repeat="item in items">
{{item["type"]}}
</th>