【问题标题】:Failover error interceptor for $http in AngularJSAngularJS 中 $http 的故障转移错误拦截器
【发布时间】:2014-02-11 13:31:21
【问题描述】:

是否可以为 $http 服务实现一个“responseError”拦截器,该拦截器只有在没有为给定的 http 承诺在别处定义的特定故障处理程序时才会触发?

这样在响应出错的情况下,下面的表达式$http.get(...).success(...)会在拦截器中执行一些代码,而下面的$http.get(...).success(...).error(...)不会。

【问题讨论】:

    标签: angularjs promise interceptor


    【解决方案1】:

    你可以使用装饰器:

    app.config(function($provide) {
        $provide.decorator('$exceptionHandler', function($delegate, $injector) {
            return function $broadcastingExceptionHandler(ex, cause) {
                $delegate(ex, cause);
                $injector.get('$rootScope').$broadcast('exception', ex, cause);
            }
        });
    });
    

    虽然此示例将捕获所有异常,但您可以检查错误并适当地处理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 2017-10-24
      • 1970-01-01
      • 2023-03-17
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多