簡體   English   中英

HTTP請求持續使用angularjs的x秒時彈出警報

[英]popup alert when http request last x seconds with angularjs

當HTTP請求未獲得任何反饋時,我想彈出並警告說“ Error Contacting Server”。

.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;

           });
    }])

您應該使用then而不是success/error方法

這是您更新的代碼

.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");
           });
        }])

您應該指定錯誤回調函數。

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

有關更多信息,請參見https://docs.angularjs.org/api/ng/service/ $ http

為此使用諾言。請在JavaScript中設置一些超時時間。

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

超時后,解決它,然后顯示彈出窗口

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM