【问题标题】:Using $http with $exceptionHandler将 $http 与 $exceptionHandler 一起使用
【发布时间】:2015-11-18 13:01:57
【问题描述】:

我想发布发生在 Angular 应用程序中的错误。

我遵循related question 中给出的方法,正如答案中所建议的,我注入了 $injector,然后从那里获得了 $http 服务。 但是这条线

未捕获的错误:循环依赖:$http

继续来。

here is the fiddle with the problem

附上相关代码:

var mod = angular.module('test', []);
mod.config(function ($provide) {
    $provide.decorator("$exceptionHandler", ['$delegate', '$injector',             function ($delegate, $injector) {
      var $http = $injector.get("$http");
    }]);
  });
mod.controller('testCtrl', function ($scope) {
  });

如果您评论该行

var $http = $injector.get("$http");

循环依赖错误消失了。

我认为我的理解中遗漏了一些东西。我究竟做错了什么?毕竟,这似乎对其他人有用。

也欢迎任何关于如何实现“将错误发布到服务”的初始目标的建议。

谢谢大家

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    你可以这样做。

    这将打破你的循环依赖错误,你也可以实现你的目标。

    如果我没有错,那么您只想在异常处理程序中调用 Web 服务。

    var httpCallOnAngularError = angular.module('exceptionTestApp', []);
    
    /*httpCallOnAngularError.factory('$exceptionHandler', function () {
     return function (exception, cause) {
            alert(exception.message);
        };
    });*/
    
    httpCallOnAngularError.config(function ($provide) {
        $provide.decorator("$exceptionHandler", function ($delegate, $injector) {
            return function (exception, cause) {
                var $rootScope = $injector.get('$rootScope');
                $delegate(exception, cause);
                $rootScope.logAngularError();
                alert(exception.message);
            };
        });
    });
    httpCallOnAngularError.run(function ($http, $rootScope) {
        $rootScope.logAngularError = function () {
            //Call your webservice here.
            $http.get("http://jsonplaceholder.typicode.com/posts/1")
                    .success(function (response) {
                        console.log(JSON.stringify(response));
                    });
        };
    
    });
    
    httpCallOnAngularError.controller('exceptionTestCtrl', function ($scope) {
        throw {message: 'Log this error by calling webservice.'};
    });
    

    您可以通过此模板验证这一点。

    <div ng-app="exceptionTestApp" ng-controller="exceptionTestCtrl">
    </div>
    

    Webservice call in Exception Handler $http in $exceptionHandler

    【讨论】:

    • 感谢萨蒂扬的热心帮助。您的代码似乎与 @maddygoround 的工作原因相同: $injector 的引用位于函数内部。我不明白为什么它会在这里工作,而不是当 $injector 在返回的函数之外时。注册装饰器或其他东西时是否有某种事件监听器?你有解释吗?
    【解决方案2】:

    在函数中包装你的注入器代码

    var mod = angular.module('test', []);
    mod.config(function ($provide) {
        $provide.decorator("$exceptionHandler", ['$delegate', '$injector',function ($delegate, $injector) {
               return function (exception, cause) {
                  var $http = $injector.get("$http");
               }
        }]);
      });
    
      mod.controller('testCtrl', function ($scope) {
      });
    

    【讨论】:

    • 是的,成功了。非常感谢。你对这种行为有什么解释吗?
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2018-10-17
    • 2020-05-24
    • 2013-06-15
    • 2023-04-01
    相关资源
    最近更新 更多