json - AngularJS 多维数组

标签 json angularjs multidimensional-array

我有以下 JSON:

{
   "records":[
      {
         "TrackID":"4",
         "ownerID":"14",
         "name":"Test1",
         "minTime":"2015-04-08T16:50:51Z",
         "maxTime":"2015-04-08T17:26:39Z",
         "tracks":[
            {
               "lat":"53.3996905",
               "long":"-2.92532816666667",
               "time":"2015-04-20T06:34:46Z",
               "hour":6
            },
            {
               "lat":"53.3997495",
               "long":"-2.92545483333333",
               "time":"2015-04-20T06:35:01Z",
               "hour":6
            },
            {
               "lat":"53.4008018333333",
               "long":"-2.9253085",
               "time":"2015-04-20T06:35:13Z",
               "hour":6
            }
         ]
      }
   ]
}

我正在尝试输出每个“ownerID”,然后是与 ownerID 关联的每个“lat”和“long”。我正在使用以下内容:

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
   $http.get("path to json")
   .success(function (response) {
       $scope.dataModel = response.records;
       });
});

然后(HTML)

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="trackInfo in dataModel">
    <td>
    {{ trackInfo.ownerID}}
    </td>
  </tr>

  <tr ng-repeat="trackDetails in trackInfo">
    <td>
    {{ trackDetails.lat }}
    </td>
  </tr>

</table>

</div>

它输出 ownerID 但不输出轨道?有谁知道为什么?谢谢...

最佳答案

那是因为您的第二次重复不知道 trackInfo 是什么。它只是在 ng-repeat 的那一刻不存在。你必须嵌套它。但是您仍然必须指定指向“轨道”的指针,否则您将迭代错误的数据集。这应该有效:

<table>
  <tr ng-repeat="trackInfo in dataModel">
    <td>
    {{ trackInfo.ownerID}}
    </td>
    <td ng-repeat="trackDetails in trackInfo.tracks">
    {{ trackDetails.lat }}
    </td>
  </tr>  
</table>

关于json - AngularJS 多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29864418/

相关文章:

javascript - json.stringify 返回 [object 对象]

Javascript 在多维数组中添加数据

c - 遍历二维数组的二维子数组的最佳方法是什么?

python - 在 Python 中存储和重新加载大型多维数据集

javascript - Backbone.js 中的嵌套模型

javascript - 将 JSON 字符串发布到 PHP 页面

java - 在 Android/Java 中使用 JSON/Base64 编码的文件

javascript - 无法绑定(bind)到没有标识符的 Controller + angularjs

javascript - 使用 TypeScript 将数组添加到 Angular 2 中的模型和数据

javascript - 如何在 Angular Controller 中使用 Browserified npm 包?