2016-06-16 44 views
0

我有這樣的代碼:如何使用角度1.x訪問對象內的數組?

<div class="list" ng-repeat="key in results"> 
    {{key.locations}} 
</div> 

JSON

[ 
     { 
     "locations": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 
     "imgs": ["111.png", "222.png"] 
     } 
    ] 

JS

.controller('ListCtrl', function($scope, $http) { 

    $http.get("js/data.json").success(function(results) { 
      $scope.results = results; 
      }); 

}) 

我要循環通過數組1,2,3 ... 任何幫助非常讚賞!

+0

另一個'ng-repeat' –

+0

我可以同時使用兩個嗎? – olivier

+0

嘗試在結果[0] .locations中輸入密鑰,然後在結果中輸入{{key}}' –

回答

0

使用另一張ng-repeat,這是嵌套在第一個:

<div class="list" ng-repeat="key in results"> 
    <div ng-repeat="location in key.locations"> 
     {{location}} 
    </div> 
</div> 

如果結果總是有1個值,只是這樣做:

<div class="list" ng-repeat="location in results[0].locations"> 
    {{location}} 
</div> 
0

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

 
app.controller('MainCtrl', function($scope) { 
 
    
 
    var test = []; 
 
    $scope.results = [ 
 
     { 
 
     "locations": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 
 
     "imgs": ["111.png", "222.png"] 
 
     } 
 
    ] 
 
    for (var i=0;i<$scope.results[0].locations.length;i++) 
 
    { 
 
     
 
     test.push($scope.results[0].locations[i]); 
 
    } 
 
    $scope.result = test; 
 
    
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script> 
 
    <script src="app.js"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <div class="list" ng-repeat="location in result"> 
 
     {{location}} 
 
    </div> 
 
    </body> 
 

 
</html>

你可以打破並創建一個臨時數組和th通過該數組遍歷。請找到該片段。並讓我知道你的發現