Javascript 2个本票一起返回无效

Javascript 2个本票一起返回无效,javascript,angularjs,angularjs-scope,angular-promise,Javascript,Angularjs,Angularjs Scope,Angular Promise,我提出了两个请求,但是当我从结果中得到值时,如果我调用承诺之外的变量,它将变为null,但是由于我依赖于两个不同承诺的请求结果,并且我需要基于每个承诺的结果执行一个函数,我不知道如何解决它 我的代码控制器: $scope.originLatLong = null; $scope.destinationLatLong = null; //Get LAT and LONG from origin and destionation http://something/{Code}

我提出了两个请求,但是当我从结果中得到值时,如果我调用承诺之外的变量,它将变为null,但是由于我依赖于两个不同承诺的请求结果,并且我需要基于每个承诺的结果执行一个函数,我不知道如何解决它

我的代码控制器:

$scope.originLatLong = null;
    $scope.destinationLatLong = null;

    //Get LAT and LONG from origin and destionation http://something/{Code}
    $http.get('something/getLatLng/'+$scope.originAirport).then(function(response){
        $scope.originLatLong = response.data; //doesnt return null

    });

$http.get('something/'+$scope.destinationAirport).then(function(response){
        $scope.destinationLatLong = response.data; //doesnt return null

    });

console.log($scope.originLatLong) //returns null
console.log($scope.destinationLatLong) //returns null
var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong);
试着这样做:

$scope.originLatLong = null;
$scope.destinationLatLong = null;

$http.get('something/getLatLng/'+$scope.originAirport).then(function(response){
    $scope.originLatLong = response.data;
    return $http.get('something/'+$scope.destinationAirport)
})
.then(function(response) {
    $scope.destinationLatLong = response.data;
    var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong);
})
或者,如果需要distanceTotal在.then()之外,请在http调用之前声明它:

$scope.originLatLong = null;
$scope.destinationLatLong = null;
var distanceTotal;

$http.get('something/getLatLng/'+$scope.originAirport).then(function(response){
    $scope.originLatLong = response.data;
    return $http.get('something/'+$scope.destinationAirport)
})
.then(function(response) {
    $scope.destinationLatLong = response.data;
    distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong);
})
编辑并解释原始问题:

$http
调用是异步的,这意味着浏览器发出请求,请求后的代码在浏览器等待服务器响应时继续运行。这意味着在您的示例中代码的执行顺序类似于

$http call
The other $http call
console.log($scope.originLatLong)
console.log($scope.destinationLatLong)
var distanceTotal = calculate($scope.destinationLatLong,$scope.originLatLong);
$scope.originLatLong = response.data;
$scope.destinationLatLong = response.data;
看到console.log()中的变量仍然为null/未定义,就很容易看出console.log为何未定义

由于混淆,另一次编辑:


您不能假定
distance total
是在
.then()
函数之外定义的。唯一可以保证定义它的地方是
then()

因为它有多个承诺,并且您希望同时使用这两个响应,所以我将使用
$q.all
解决这个问题

我们所需要做的就是创造一系列承诺。使用
$q.all
,我们可以在一个
中得到两个承诺的响应。以下是方法:

var promises = [];
promises.push($http.get('something/getLatLng/'+$scope.originAirport));
promises.push($http.get('something/'+$scope.destinationAirport));

$q.all(promises).then(function(response) {
  $scope.originLatLong = response[0].data;
  $scope.destinationLatLong = response[1].data;

  console.log($scope.originLatLong) 
  console.log($scope.destinationLatLong) 
  var distanceTotal = calculate($scope.destinationLatLong, $scope.originLatLong);
  ...
});

谢谢Fissio,你能告诉我我做错了什么或者为什么不工作吗?你好,我做了一个控制台。log(distanceTotal)在承诺之外没有显示,给了我一个未定义的“检查我的编辑;问题可能是相同的。你需要使用
distanceTotal
将代码放在
中。然后()。