【发布时间】:2016-05-17 02:36:52
【问题描述】:
我正在尝试执行嵌套 ng-repeat(使用 ng-repeat-start 和 ng-repeat-end)。
第一个重复显示位置列表以及关联的 ID。例如:
Mauritius | ID: po8j3mau72
Dubai | ID: hyf53ygt65
Sri Lanka | ID: gytf87hg5d
第二个重复“应该”显示在每个位置花费的一些金额。但是,重复应该使用上述重复中的 _id 并使用在端点中传递的 _id 执行 HTTP.GET,例如
http://myserver/endpoint/gytf87hg5d
我可以让第一个列表出现,并对每个_id执行一个http请求,但我不知道如何显示数量,例如:
Mauritius | ID: po8j3mau72
900, 900, 900, 900
Dubai | ID: hyf53ygt65
Sri Lanka | ID: gytf87hg5d
控制器和服务:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, myService) {
$scope.test = 'hello';
myService.getItemModel(function(itemModel) {
$scope.myItem = itemModel;
var itemList = itemModel.items;
for (var i = 0; i < itemList.length; i++) {
var addressId = itemList[i]._id;
myService.getContentModel(addressId)
.success(function (data, status, headers, config) {
$scope.contents = data;
console.log($scope.contents);
})
.error(function (data, status, headers, config) {
});
}
});
});
app.factory('myService', function($http, $q) {
return {
getItemModel: function(itemModel) {
$http.get('itemID.json')
.success(function(data) {
itemModel(data);
})
.error(function(error) {
alert('An error occured whilst trying to retrieve your item data');
});
},
getCotnentModel: function(addressId) {
return $http({
method: 'GET',
url: addressID + '.json',
headers: {'Content-Type': 'application/json'}
});
}
}
});
【问题讨论】:
-
您可以使用
$scope.contents.push({"id":addresssID,"amount":data})推送到数组
标签: javascript angularjs angularjs-scope angularjs-ng-repeat angularjs-service