繁体   English   中英

从json数组获取数据到Table angularJS

[英]get data from json array to Table angularJS

我有这个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

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

您的控制器:

.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

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

采用

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

要么

console.log($scope.currentPageStores);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM