[英]How can I access an array inside an object with angular 1.x?
I have this code: 我有这个代码:
<div class="list" ng-repeat="key in results">
{{key.locations}}
</div>
JSON 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 JS
.controller('ListCtrl', function($scope, $http) {
$http.get("js/data.json").success(function(results) {
$scope.results = results;
});
})
I want to loop trough the array 1,2,3... Any help much appreciated! 我想循环通过阵列1,2,3 ...任何帮助非常感谢!
Use another ng-repeat
, which is nested in the first one: 使用另一个ng-repeat
,它嵌套在第一个中:
<div class="list" ng-repeat="key in results">
<div ng-repeat="location in key.locations">
{{location}}
</div>
</div>
If results always has 1 value, just do this: 如果结果总是有1个值,请执行以下操作:
<div class="list" ng-repeat="location in results[0].locations">
{{location}}
</div>
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="angular.js@1.2.x" 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>
You can break and create a temp array and then iterate through that array. 您可以中断并创建临时数组,然后遍历该数组。 Please find the snippet. 请找到代码段。 And let me know your findings 让我知道你的发现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.