【问题标题】:how to precent http interceptors if offline离线时如何防止http拦截器
【发布时间】:2014-12-07 00:56:11
【问题描述】:

我正在使用 ionic 构建一个应用程序,并且在每个加载的请求上都显示一个加载指示器。当请求完成时,加载指示器被移除。当存在网络连接时,这非常有用。

离线时,请求开始并显示加载指示器。问题是加载指示器永远不会消失,因为请求永远不会完成。我怎样才能最好地处理这个?我在考虑超时什么的。最好的方法是在用户离线时根本不启动请求。

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

【问题讨论】:

  • 你也应该尝试处理 requestError 和 responseError。

标签: angularjs angular-http-interceptors


【解决方案1】:

在下面添加

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


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

【讨论】:

  • 太棒了,不知道那些事件!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2018-07-25
相关资源
最近更新 更多