在Angularjs中读取嵌套的json

时间:2016-03-18 11:03:43

标签: angularjs json

我是AngularJS的新手。我有JSON文件,我必须使用angularjs获取!

JSON文件

{
  "records": [
    {
      "students": [
        {
          "id": 1,
          "name": "Bill",
        },
        {
          "id": 2,
          "name": "Steve",
        }
        ],
      "exstudent": [
        {
          "id": 1,
          "name": "Woz",
        },
        {
          "id": 2,
          "name": "Jonathan",
        }
        ]
    }
    ]
}

控制器部分摘要 -

 $http.get('json/somedata.json').success(function (data){

        $scope.name = data.records.student.name;
        $scope.exname = data.records.exstudent.name;

        });


  }])

HTML

<div class="panel-body" ng-repeat = "browse in name">
  {{browse.student.name}}
</div>

我做错了哪一部分?我认为问题在于ng-repeat! 需要帮助

1 个答案:

答案 0 :(得分:2)

由于data.records.students.namestudents,您无法使用Array

然而,您可以将其数据存储到$scope并在ng-repeat中使用

$http.get('json/config.json').success(function (data){
    $scope.students = data.records.students;
    $scope.exstudents = data.records.exstudent;
});

然后在您的HTML中使用ng-repeat,如下所示:

<div class="panel-body" ng-repeat="student in students">
    {{student.name}}
</div>