【发布时间】:2017-07-25 13:51:14
【问题描述】:
首先我想使用 $http 来接收一些数据(例如学生),然后我想进行另一个 $http 调用来获取例如学生详情。之后,我想将部分 studentDetails 附加到学生 JSON 中。 我还需要第一次调用的响应才能为第二次调用创建 url。
问题是我无法访问另一个内部第一个 http 调用的响应。 有谁知道如何做到这一点?
var getStudents = function(){
var deferred = $q.defer();
$http.get("https://some_url")
.success(function(response){
deferred.resolve(response);
}).error(function(errMsg){
deferred.reject(errMsg);
});
return deferred.promise;
}
var appendStudentDetails = function(){
getStudents().then(function(response){
var studentsWithDetails = response;
for(var i=0; i<studentsWithDetails.length; i++){
$http.get("some_url/"+studentWithDetails[i].user.details+"/")
.success(function(res){
//here I want to append the details,
//received from the second http call, to each student
//of the array received from the first http call
//PROBLEM: I cannot access response of the
//first http call inside the another
})
}
})
【问题讨论】:
-
您可以简单地将较早的 http 调用响应存储在您的作用域变量中,并在下一次调用的成功回调中使用它。
-
应该是:
var studentsWithDetails = response.data; -
响应中存在什么?
-
@Vivz 内部响应我有 20 名学生组成的数组,其中包含一些信息,但是从第二个 http 调用中,我得到了一些其他信息,我想将这些信息附加到数组的每个元素中
-
你确定studentsWithDetails是一个数组吗?你能安慰检查一下吗
标签: angularjs json http promise angular-promise