gpt4 book ai didi

javascript - angularjs:使用来自两个 http 请求的变量

转载 作者:行者123 更新时间:2023-11-30 16:23:05 24 4
gpt4 key购买 nike

我想使用来自两个 http 请求的变量。我尝试使用 rootScope,但它不起作用。 dlondlat 仍未定义。这是代码。

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope,$rootScope, $http) {
$http.get("http://api.zippopotam.us/us/68503")
.then(function (response) {
$rootScope.lon1 = response.data.places[0].longitude;
$rootScope.lat1 = response.data.places[0].latitude;
});
$http.get("http://api.zippopotam.us/us/68504")
.then(function (response) {
$rootScope.lon2 = response.data.places[0].longitude;
$rootScope.lat2 = response.data.places[0].latitude;
});
$scope.dlon = $rootScope.lon1 - $rootScope.lon2;
$scope.dlat = $rootScope.lat1 - $rootScope.lat2;
});

谢谢。

最佳答案

您将需要使用一个 promise ,该 promise 将在两个 http 请求完成后解析。在 Angular 中,这是用 $q 完成的。

关于javascript - angularjs:使用来自两个 http 请求的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com