Search code examples
angularjsangular-http-interceptors

how to precent http interceptors if offline


I'm building an app with ionic and I'm showing a loading indicator on every request that loads. When the request finishes, the loading indicator is removed. This works great when a network connection is present.

When offline, the request starts and the loading indicator is show. Problem is that the loading indicator never disappears because the request never finishes. How can I best handle this? I'm thinking about a timeout or something. The best way would be not to start the request at all when a user is offline.

$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
      }
    }
  })

Solution

  • add below

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