2014-12-06 85 views
-1

我使用離子建立應用程序,並且在加載的每個請求上都顯示加載指示器。當請求結束時,加載指示器被刪除。這在網絡連接存在時很有用。如何脫機使用http攔截器如果脫機

當離線時,請求開始並顯示加載指示符。問題是加載指示符永遠不會消失,因爲請求永遠不會結束。我該如何最好地處理這個問題?我正在考慮超時或什麼的。當用戶離線時,最好的方法是不要啓動請求。

$httpProvider.interceptors.push(function($rootScope) { 
    return { 
     request: function(config) { 
     config.timeout = 5000; 
     $rootScope.$broadcast('loading:show') 
     return config 
     }, 
     response: function(response) { 
     $rootScope.$broadcast('loading:hide') 
     return response 
     } 
    } 
    }) 
+2

你也應該嘗試處理requestError和responseError。 – OdeToCode 2014-12-06 16:22:21

回答

0

下面添加

responseError: function(response) { 
    $rootScope.$broadcast('loading:hide') 
    return response 
    } 


    requestError: function(response) { 
    $rootScope.$broadcast('loading:hide') 
    return response 
    } 
+0

太棒了,不知道這些事件! – Jorre 2014-12-07 14:36:30