我有一个api('rest / latest / testruns / 16543558')id是test run ids。我想在oneshot中使用不同的id进行调用,并在我的视图中将值存储在对象中以进行ng-repeat。
$http
.get('rest/latest/testruns/' + testplanid)
.success(function (response) {
$scope.testruns = response;
})
.error(function (data, status) {
});
答案 0 :(得分:1)
可以使用$q.all
一次发送多个http请求。
var ids = ['16543558', '16543559', '165435510']
var fullArr = [];
for (id in ids) {
fullArr.push($http.get("rest/latest/testruns/" + id))
}
$q.all(fullArr).then(function(arrayOfResults) {
//return the response of all equest
});
答案 1 :(得分:1)
您可以使用下面的$q.all来实现类似的行为
var ids=[1,2,3,4,5], promises;
promises = ids.map(function(v){
return $http
.get('rest/latest/testruns/' + v)
});
$q.all(promises).then(function(responses){
// responses[0] => id 1
// responses[1] => id 2
// responses[2] => id 3
// responses[3] => id 4
// responses[4] => id 5
})
答案 2 :(得分:0)
您可以使用$ q.all来实现多个承诺调用。
HTML code:
<div ng-app>
<div ng-controller="PromiseCtrl">
{{result}}
</div>
</div>
控制器功能:
function PromiseCtrl($scope, $q, $timeout) {
// Our promise function with its delay as argument
var getPromise = function(delay) {
// Creates a Deferred object
var deferred = $q.defer();
$timeout(function() {
// Resolve the promise at the end of the delay if said delay was > 0
if(delay > 0) {
deferred.resolve("Success");
} else {
deferred.reject("Fail");
}
}, delay);
// The promise of the deferred task
return deferred.promise;
};
// Init
$scope.result = "Waiting";
/*
* Combines multiple promises into a single promise
* that will be resolved when all of the input promises are resolved
*/
$q.all([
getPromise(1000),
getPromise(2000),
getPromise(3000) // <--- Try something less than 0
]).then(function(value) {
// Success callback where value is an array containing the success values
$scope.result = value;
}, function(reason) {
// Error callback where reason is the value of the first rejected promise
$scope.result = reason;
});
}