从json数组获取数据到表angularJS

时间:2016-03-03 16:18:03

标签: javascript angularjs json

我有这个json数据:

     {
status: "SUCCESS",
data: [
{
FirstName: "Student ",
LastName: "1",
Id: 1,
ObjectState: 0
},
{
FirstName: "Student ",
LastName: "2",
Id: 2,
ObjectState: 0
    }
   }
  ]
}

我在我的控制器和视图中尝试过这样的事情: app.js:

.controller("etudCtrl",["$scope", "$http", function ($scope, $http) {
        var i;

    $http({method: 'GET', url: 'MyURL'})
             .success(function (data) {

                for(i=0;i<data.length;i++){
                $scope.paged = data[i].data; // response data 
                console.log($scope.paged+" $scope.paged");
        }
            $scope.currentPageStores= $scope.paged;
            console.log($scope.currentPageStores+"  values");

              console.log("success");
    }).error(function (data, status, headers, config) {
              console.log("data error ...");
     });
 }])

Students.html:

<table>
<thead>...</thead>
 <tbody data-ng-controller="etudCtrl">
   <tr ng-repeat="store in currentPageStores" >    
  <td align="center">{{store.LastName}}</td>
  <td align="center">{{store.FirstName}}</td>
  </tr>
</tbody>
</table>

这就是我在控制台中获得的内容:

values
success

我在控制台或表格中没有得到任何数据:( 请帮忙 感谢

更新: 我试着用这个:

 $rootScope.usersData = angular.toJson(data.data);
 console.log($rootScope.usersData+" my data");

我获取了我想在控制台中显示的所有数据

UPDATE2:

 $http({method: 'GET', url: 'MyURL'})
             .success(function (data) {

       console.log(JSON.stringify(data)+"myData");

        for(i=0;i<data.length;i++){
        $scope.paged = data.data[i]; // response data 
        console.log($scope.paged+" $scope.paged");
              }
.....
}

我在控制台中得到了这个:

{"status":"SUCCESS","data":[{"FirstName":"Student ","LastName":"1","Id":1,"ObjectState":0},{"FirstName":"Student ","LastName":"2","Id":2,"ObjectState":0}]}myData

2 个答案:

答案 0 :(得分:1)

无需在控制器中循环。请尝试以下方法:

您的控制器:

.controller("etudCtrl",["$scope", "$http", function ($scope, $http) {


    $http({method: 'GET', url: 'MyURL'})
             .success(function (data) {
            $scope.currentPageStores= data.data;
            console.log($scope.currentPageStores+ "  values");

              console.log("success");
    }).error(function (data, status, headers, config) {
              console.log("data error ...");
     });
 }])  

您的student.html

<table>
<thead>...</thead>
 <tbody data-ng-controller="etudCtrl">
   <tr ng-repeat="store in currentPageStores track by $index" >    
  <td align="center">{{store.LastName}}</td>
  <td align="center">{{store.FirstName}}</td>
</tbody>
</table>  

以下是 PLUNKER 示例,说明当数据作为您的数据进入数组时的访问方式:http://plnkr.co/edit/Q9ewbH7XevsOQG0Yzv6B?p=preview

答案 1 :(得分:0)

您无法使用其他字符串在控制台上打印对象。

使用

console.log(JSON.stringify($scope.currentPageStores)+"  values");

console.log($scope.currentPageStores);