我有这两个函数,这是代码示例
var getRangeTime = function(numSerie){
$http.get("data/tempsgammes.json").then(function(res){
return $scope.time = res.data[numSerie].temps
})}
var updateScreen = function() {
$http.get("http://localhost:5000").then(function(response){
$scope.numSerie = response.data.refDetail.Num_Serie
$scope.numTeam = response.data.team
$scope.t = getRangeTime($scope.numSerie)
//Rest of code the code
}
这是我想做的:
首先,我调用函数updateScreen()来获取序列号(numSerie),然后根据numSerie的值>,另一个函数getRangeTime()将被执行来获取时间,然后函数updateScreen()将使用getRangeTime()的结果继续执行。
这里的问题是异步的,我的意思是 updateSreen() 应该等待 getRangeTime() 直到她返回所需的值,这是一种异步 并等待。
我已经尝试过,但它不起作用,实际上我不太知道如何使用它们,我在这里搜索并尝试了现有的解决方案,但效果也不太好,我总是得到未定义。
谁能帮我吗?
最佳答案
在这种情况下,您应该使用 promise 链来依次调用请求。
function updateScreen(){
return $http.get("http://localhost:5000")
}
function getRangeTime(){
return $http.get("data/tempsgammes.json")
}
$scope.calltwoFunctions = function() {
updateScreen()
.then( function( response )
{
$scope.numSerie = response.data.refDetail.Num_Serie
$scope.numTeam = response.data.team
return getRangeTime();
})
.then(function(response){
console.log(response.data);
})
}
关于javascript - AngularJs : $http chaining,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45788622/