在http.get函数返回之前调用的Angular成功函数

时间:2018-06-16 16:33:12

标签: javascript angularjs promise angular-promise

我有一个生成HTTP GET请求的角度控制器,并且在promise返回时,调用成功的错误函数。我的代码现在有时会在GET请求返回后执行成功函数,有时会在它之前执行。我的承诺处理中有什么东西搞砸了吗?需要注意的是,有时甚至不会调用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);
            }
        );
    };
}

1 个答案:

答案 0 :(得分:0)

我发现这种情况正在发生,因为Angular会缓存GET请求一段时间,并且正在使用我的请求的缓存版本。