2

我有一个带有 AngularJS 1.3 单页应用程序组件的 MVC 5.0 Web 项目。如果发生任何未处理的角度异常,我想发回 MVC 控制器,所以我像这样覆盖 $exceptionHandler 工厂

    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%3C-%20%24rootScope" 当我加载包含此脚本的页面时。显然,$http 已经对 $exceptionHandler 有依赖,所以我不能给 $exceptionHandler 对 $http 的依赖,因为这会产生循环依赖。

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

4

1 回答 1

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
        });
    };
于 2014-11-11T15:57:39.767 回答