2016-06-16 6 views
0

Ich habe diesen Code:Wie kann ich auf ein Array innerhalb eines Objekts mit eckiger 1.x zugreifen?

<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; 
      }); 

}) 

ich Schleife wollen Trog die Array 1,2,3 ... Jede Hilfe sehr geschätzt !

+0

ein anderes 'ng-repeat' –

+0

kann ich zwei gleichzeitig verwenden? – olivier

+0

versuchen Sie 'key in results [0] .locations' und dann' {{key}} ' –

Antwort

0

Verwenden Sie ein weiteres ng-repeat, die in der ersten verschachtelt ist:

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

Wenn die Ergebnisse immer Wert 1 hat, gerade dies tun:

<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>

Sie können brechen und ein Temp-Array und th erstellen ie durchlaufen dieses Array. Bitte finden Sie das Snippet. Und lassen Sie mich Ihre Ergebnisse wissen

Verwandte Themen