2017-06-04 15 views
1

我嘗試使用Angular和ng-repeat從Ajax json加載HTML表格。 我的問題是,JSON有點複雜,我不知道如何訪問「informationstyp」,這是我想要在表中顯示。 JSON的根(「值」)是一個列表。下一級(「informationstyper」)也是一個列表。使用angular和ng-repeat從Ajax JSON加載表格

如何將「informationstyp」放入表格中? 我應該寫什麼而不是{{data.arendeslagId}}在我的HTML?

我想在我的表是什麼:

FK1002

FK1003

FK1004

謝謝您的幫助。

我的HTML:

 <table class="table table-hover"> 
      <thead> 
      <tr> 
       <th>Arendeslagid</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="data in infotyper"> 
       <td>{{data.arendeslagId}}</td> 
      </tr> 
      </tbody> 
     </table> 

我的javascript:

$scope.infotyper = ""; 

    $scope.invoke = function() { 
     $.ajax({ 
       "cache": false, 
       "async": true, 
       "crossDomain": true, 
       "url": url", 
       "method": "POST", 
       "headers": { 
        "cache-control": "no-cache", 
       }, 
      success: function (response) { 
        $scope.infotyper= response; 
       } 
      }); 
     }; 

我的JSON響應:

enter image description here

+0

您可以在角度而不是jQuery的AJAX使用$ http服務的方法。 – hasan

+0

@hasan謝謝你,我現在已經把它改成了$ http – chichi

回答

3

試圖改變這一點:

success: function (response) { 
    $scope.infotyper= response; 
} 

要這樣:

success: function (response) { 
    $scope.infotyper= response.values; 
    $scope.$apply(); 
} 

如果您正在使用AngularJS我強烈建議你使用$http服務,而不是jQuery的AJAX的請求

+0

這也行不通。我如何才能進入JSON的下一個級別(「informationstyper」)? – chichi

+0

對於$ scope,一個up-vote。$ apply() – chichi

+0

該字段是一個數組。所以你需要另外一個ng-repeat來循環這些元素。例如:' ​​{{data.arendeslagId}} {{typer.informationstyp}} ' – quirimmo