2016-03-20 48 views
0

我想從沒有鍵的JSON中獲取數據到表中。從表中無鍵獲取JSON值

[ 
[ 
"valu10", //row1 col1 
"valu11", //row1 col2 
"valu12", 
"valu13" 
], 
[ 
"valu20", 
"valu21", 
"valu22", 
"valu23" 
], 
[ 
"valu30", 
"valu31", 
"valu32", 
"valu33" 
] 
] 

在我的成功塊中,我獲取了警報框中的數據。我怎樣才能得到這些數據在角度表中。我試着做以下,

........ 
}).success(function(data, status, headers, config) { 
     var log = [];   

     var data1 = angular.fromJson(eval(data)); 
     var rowList = data1; 
     $scope.rows = rowList; 
    }).error(function(data, status, headers, config) { 
     alert("error");  
    }); 
在HTML

    <table class="table table-striped"> 
         <thead> 
          <tr> 
           <th ng-repeat="header in headers">{{header}}</th> 
          </tr> 
         </thead> 
         <tbody> 

          <tr ng-repeat="item in rows"> 
          {{item}} 
          </tr> 
         </tbody> 
        </table>  
+0

您在標題重複標題時,有沒有$ scope.headers。 –

回答

1

我已經創建了一個對的jsfiddle你,希望它會幫助你:

https://jsfiddle.net/oronbdd/h4sor3w4/1/

Angualr控制器:

var app = angular.module('plunker', []); 

app.factory('myService', function($http) { 
    return { 
    async: function() { 
     return $http.get('https://api.myjson.com/bins/368hv'); 
    } 
    }; 
}); 

app.controller('MainCtrl', function(myService,$scope) { 
    $scope.data = "oron"; 
    myService.async().then(function(d) { 
    $scope.data = d.data; 
    }); 
}); 

HTML:

<div ng-app="plunker"> 
<div ng-controller="MainCtrl"> 
     JSON:{{data}}<br> 

    <br/> 
    ANSWER: 

    <table class="table table-striped"> 
    <thead> 
     <tr> 
     <th ng-repeat="x in data">{{$index}}</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr ng-repeat="array in data"> 
     <td ng-repeat="item in array"> 
      {{item}} 
     </td> 
     </tr> 
    </tbody> 
    </table>  

</div> 

+0

如果需要,我可以隱藏項目中的某一列嗎?在這種情況下僅顯示2列。 – NaaN

+1

你有2個選項:1.刪除javascript對象中的列,在我們的例子中是$ scope.data對象。 2.使用ng-if語句,例如:ng-if =「$ index!== 1」 –