如何在全局错误处理中使用$ http.post?

时间:2014-11-11 15:30:50

标签: angularjs angularjs-http

我有一个带AngularJS 1.3单页应用程序组件的MVC 5.0 Web项目。如果出现任何未处理的角度异常,我想回发给MVC控制器,所以我{m} {m} overriding the $exceptionHandler factory这样:

    mod.factory('$exceptionHandler', ['$http', function($http) {
    return function(exception, cause) {
        console.log(exception.message);
        $http.post("Application/CatchAngularError", {
            exception: exception,
            cause: cause
        });
    }
}]);

在Chrome中,Javascript控制台报告"未捕获错误:[$ injector:cdep] http://errors.angularjs.org/1.3.0/ $ injector / cdep?p0 =%24rootScope%20%3C-%20%24http%20%3C - %20%24exceptionHandler%20%3 C-%20%24rootScope"当我加载包含此脚本的页面时。显然,$ http已经依赖于$ exceptionHandler,所以我不能给$ exceptionHandler一个依赖于$ http,因为这会创建一个循环依赖。

我想我可以使用jQuery.ajax,但如果存在,我更喜欢更有角度的解决方案。有人有吗?

1 个答案:

答案 0 :(得分:1)

您可以使用$ injector服务并查找$ http服务:

mod.factory('$exceptionHandler', ['$injector', function($injector) {
    return function(exception, cause) {
        console.log(exception.message);
        var $http = $injector.get('$http');
        $http.post("Application/CatchAngularError", {
            exception: exception,
            cause: cause
        });
    };