Angular 在 http.get 函数 returns 之前调用的成功函数
Angular success function called before http.get function returns
我有一个 angular 控制器,它发出 HTTP GET 请求并承诺 return,调用错误函数成功。我现在的代码有时会在 GET 请求 return 之后执行成功函数,有时会在它之前执行。我的承诺处理有什么问题吗?需要注意的是,有时 http GET 甚至没有被调用(或者至少它的断点)并且成功函数无论如何都会发生。这是我的代码:
.controller('CheckOutCtrl', ['$scope', '$http', function($scope, $http) {
//Controller for checkout feature
$scope.owner = "Unclaimed"; //Default owner
$scope.endDate = "";
$scope.unclaimed = true;
$scope.showpopup = false; //Should we show the popup that checkout is successful or returned
$scope.currentOwner = false; //Show return date
$scope.popuptext = "";
$scope.checkOut = function(){ //Extends or creates a check out
$http.get("/checkout/extend/code/" + code + "/username/" + userName).then(
function success(response, status, headers, config) {
console.log("Checked out or extended");
$scope.showpopup = true;
$scope.currentOwner = true;
$scope.popuptext = response.data; //Show airport checked out until or error
console.log($scope.popuptext);
init(); //Update current owner
},
function error(response, status, headers, config) {
$scope.error = response;
console.error("Check out error:", response);
}
);
};
}
我发现这是因为 Angular 将 GET 请求缓存了一段时间,并且正在使用我的请求的缓存版本。
我有一个 angular 控制器,它发出 HTTP GET 请求并承诺 return,调用错误函数成功。我现在的代码有时会在 GET 请求 return 之后执行成功函数,有时会在它之前执行。我的承诺处理有什么问题吗?需要注意的是,有时 http GET 甚至没有被调用(或者至少它的断点)并且成功函数无论如何都会发生。这是我的代码:
.controller('CheckOutCtrl', ['$scope', '$http', function($scope, $http) {
//Controller for checkout feature
$scope.owner = "Unclaimed"; //Default owner
$scope.endDate = "";
$scope.unclaimed = true;
$scope.showpopup = false; //Should we show the popup that checkout is successful or returned
$scope.currentOwner = false; //Show return date
$scope.popuptext = "";
$scope.checkOut = function(){ //Extends or creates a check out
$http.get("/checkout/extend/code/" + code + "/username/" + userName).then(
function success(response, status, headers, config) {
console.log("Checked out or extended");
$scope.showpopup = true;
$scope.currentOwner = true;
$scope.popuptext = response.data; //Show airport checked out until or error
console.log($scope.popuptext);
init(); //Update current owner
},
function error(response, status, headers, config) {
$scope.error = response;
console.error("Check out error:", response);
}
);
};
}
我发现这是因为 Angular 将 GET 请求缓存了一段时间,并且正在使用我的请求的缓存版本。