【问题标题】:Angular.js - Nested JSON scope with AngularAngular.js - 带有 Angular 的嵌套 JSON 范围
【发布时间】:2014-09-23 13:01:18
【问题描述】:
{
  "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 为:

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

但我没能成功。我做错了什么?

编辑:jsfiddler

【问题讨论】:

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


    【解决方案1】:

    我看你根本没有使用导入的属性,试试

    $scope.items = data.imports.imported;
    

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

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

    【讨论】:

      【解决方案2】:

      你的 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>
      

      【讨论】:

      • 那么我在哪里可以添加导入的日期?
      • 你可以尝试嵌套你的 ng-repeat: {{obj["date"]}} {{item["type"] }} 参考这里:stackoverflow.com/questions/21161922/…
      • 我想我越来越近了
      猜你喜欢
      • 2019-02-19
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2010-12-18
      • 2019-05-07
      • 1970-01-01
      相关资源
      最近更新 更多