Angularjs 角度多递归$http查询
我使用角度1.5 我查询第一个web服务(“/institutions”),该服务返回机构列表:Angularjs 角度多递归$http查询,angularjs,Angularjs,我使用角度1.5 我查询第一个web服务(“/institutions”),该服务返回机构列表: http://localhost:8080/erecolnat/v1/institutions [ { "institutionid" : 9, "institutioncode" : "FEN", "links" : [{ "rel" : "self", "href" : "h
http://localhost:8080/erecolnat/v1/institutions
[ {
"institutionid" : 9,
"institutioncode" : "FEN",
"links" : [{
"rel" : "self",
"href" : "http://localhost:8080/erecolnat/v1/institutions/9"
}, {
"rel" : "collections",
"href" : "http://localhost:8080/erecolnat/v1/institutions/9/collections"
}
]
}, {
"institutionid" : 10,
"institutioncode" : "MHNAIX",
"links" : [{
"rel" : "self",
"href" : "http://localhost:8080/erecolnat/v1/institutions/10"
}, {
"rel" : "collections",
"href" : "http://localhost:8080/erecolnat/v1/institutions/10/collections"
}
]
},
..........
]
现在,我需要每个机构查询“/institutions/XX/collections”web服务,以获取该机构的集合并将数据保存在机构内部
http://localhost:8080/erecolnat/v1/institutions/4/collections
[ {
"@collectionID": 1,
"collectioncode": "CHE",
"type": "h",
"collectionName": "Herbarium specimens of Société des Sciences Naturelles et Mathématiques de Cherbourg (CHE)",
"links": [{
"rel": "self",
"href": "http://localhost:8080/erecolnat/v1/collections/4"
}]
} ]
问题是谁将参数传递给第二个$http以知道在何处保存数据,如下所示:
angular.module('interfaceApp').service('services', function($rootScope,$http,$q ) {
this.getInstitutions = function(){
var getHrefCollections = function(links){
for(var l in links){
if(links[l]["rel"] == "collections"){
return links[l]["href"];
}
}
return null ;
};
var url = $rootScope.api +"institutions";
return $http({
method: 'GET',
url: url
}).then(function successCallback(response) {
for(var i in response.data){
var url = getHrefCollections( response.data[i]["links"] );
$http({ method: 'GET', url: url }).then(
function successCallback(response /* how to pass a parameter her */) {
console.log(response.data); // OK
// how to put this data in the good place ( at response.data[i]["collections"] for example )
}
);
}
return response ;
}, function errorCallback(response) {
console.log(response) ;
});
}
});
如果我理解正确,我将执行以下操作:
var url = $rootScope.api +"institutions";
return $http({
method: 'GET',
url: url
}).then(
function successCallback(response) {
var promises = [];
for(var i in response.data){
var url = getHrefCollections( response.data[i]["links"] );
promises.push( //Push the promises into an array
$http({ method: 'GET', url: url })
);
}
return $q.all(promises); // Resolve all promises before going to the next .then
},
function errorCallback(response) {
console.log(response);
}
).then(
function(resultArray) {
console.log(resultArray); // resultArray is an array (IN ORDER OF CALLING) with the results from all the promises
},
function(error) {
console.log(error);
}
);
因此,在您的resultArray中,结果的索引仍将与您在
successCallback
中调用的索引相同,这样您可以将其链接回您的原始机构如果我理解正确,我将执行以下操作:
var url = $rootScope.api +"institutions";
return $http({
method: 'GET',
url: url
}).then(
function successCallback(response) {
var promises = [];
for(var i in response.data){
var url = getHrefCollections( response.data[i]["links"] );
promises.push( //Push the promises into an array
$http({ method: 'GET', url: url })
);
}
return $q.all(promises); // Resolve all promises before going to the next .then
},
function errorCallback(response) {
console.log(response);
}
).then(
function(resultArray) {
console.log(resultArray); // resultArray is an array (IN ORDER OF CALLING) with the results from all the promises
},
function(error) {
console.log(error);
}
);
因此,在您的resultArray中,结果的索引仍将与您在
successCallback
中调用的索引相同,这样您就可以将其链接回您的原始机构您比我更快!你比我快,陛下!