gpt4 book ai didi

javascript - 装饰 $errorProvider 时的循环引用

转载 作者:行者123 更新时间:2023-11-30 12:26:12 31 4
gpt4 key购买 nike

在我正在进行的项目中,我正在按照项目负责人的要求实现开发人员通知系统。它的工作方式是,如果发生前端错误,开发团队会收到一封错误电子邮件。

但是,在我当前的实现中,我似乎有以下循环依赖:

$rootScope <- $http <- $exceptionHandler <- $rootScope

在下面的代码中:

(function() {
'use strict';

// Using .config to 'decorate' the exception handler.
angular.module('app').config(function($provide) {
$provide.decorator('$exceptionHandler', ['$delegate', '$http', dispatchErrorEmail]);
});

function dispatchErrorEmail($delegate, $http) {
return function (exception, cause) {
// Execute default implementation.
$delegate(exception, cause);

// Angular exceptions fail softly, but generate an error email.
var args = {
'exception': exception,
'cause': cause
};
$http.post('/api/admin/ErrorNotification', args);
};
}
})();

如您所见,有一个问题:我实际上并没有使用$rootScope以任何方式装饰我的$errorHandler .

更重要的是,$provide.decorator 都不是或 $errorHandler文档记录了 $rootScope被隐式包含。

问题:

  1. 怎么样$rootScope注入(inject)这项服务,对吗?
  2. 我可以用什么方式重写我的 $exceptionHandler装饰以避免这种循环依赖?

最佳答案

再四处看看——特别是在相关侧边栏上——让我找到了this answer .差不多,我必须使用 $injector 来获取我的 $http 服务的实例句柄。

(function() {
'use strict';

// Using .config to 'decorate' the exception handler.
angular.module('app').config(function($provide) {
$provide.decorator('$exceptionHandler', ['$delegate', '$injector', dispatchErrorEmail]);
});

function dispatchErrorEmail($delegate, $injector) {
return function (exception, cause) {
// Execute default implementation.
$delegate(exception, cause);

// Angular exceptions fail softly, but generate an error email.
var $http = $injector.get('$http');
var args = {
'exception': exception,
'cause': cause
};
$http.post('/api/admin/ErrorNotification', args);
};
}
})();

这并不能解释为什么 $rootScope 会潜入$exceptionHandler 服务;我想我只需要相信它确实如此。

关于javascript - 装饰 $errorProvider 时的循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289187/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com