简体   繁体   中英

popup alert when http request last x seconds with angularjs

I want to popup and alert saying "Error Contacting Server" when http request doesn't get any feedback.

.controller('items_ctrl',['$scope','$http',function($scope,$http){
    $scope.shop_id=localStorage.getItem("shop_id");
        $http.get('http://localhost/myapp/spree/stocks.php').success(function(data){
            console.log(JSON.stringify(data));
            $scope.item=data;

           });
    }])

You should use then instead of success/error methods

Here is your updated code

.controller('items_ctrl',['$scope','$http',function($scope,$http){
    $scope.shop_id=localStorage.getItem("shop_id");
        $http.get('http://localhost/myapp/spree/stocks.php').then(function(data){
               console.log(JSON.stringify(data));
               $scope.item=data;

           }, function(error){
               alert("Error contacting server");
           });
        }])

You should specify error callback function.

$http.get('/someUrl', config).then(successCallback, errorCallback);

For more information please see https://docs.angularjs.org/api/ng/service/ $http

Use promise for it .Set some timeout in your javascript.

var cancelReq= $q.defer();
$http.get('/someUrl', {timeout: cancelReq.promise}).success(successCallback);
$timeout(showErrorPopup, 3000);

after timeout , resolve it and then show popup

function showErrorPopup(){
   cancelReq.resolve(); 
//popup code
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM