装饰 $errorProvider 时的循环引用
Circular Reference when decorating $errorProvider
在我正在进行的项目中,我正在按照项目负责人的要求实施开发人员通知系统。它的工作方式是,如果发生前端错误,开发团队会收到一封错误电子邮件。
但是,对于我当前的实现,我似乎有以下循环依赖:
$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
被隐式包含。
问题:
$rootScope
是如何注入此服务的?
- 我可以用什么方式重写我的
$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
服务;我想我只需要相信它确实如此。
我遇到了类似的问题,您的 post 帮助我找到了解决方案。我有一个公共库,几乎用于我所有的公共页面活动,例如显示 toast 和管理加载指示器。您的解决方案帮助使用注入器访问该服务。希望这对其他人有帮助!
// custom exception handling
module.config(function ($provide) {
$provide.decorator('$exceptionHandler', ['$delegate', '$injector', function ($delegate, $injector) {
return function (exception, cause) {
$delegate(exception, cause);
var common = $injector.get('common');
common.toast('Whoops, an error occurred!');
common.loading(false);
};
}]);
});
在我正在进行的项目中,我正在按照项目负责人的要求实施开发人员通知系统。它的工作方式是,如果发生前端错误,开发团队会收到一封错误电子邮件。
但是,对于我当前的实现,我似乎有以下循环依赖:
$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
被隐式包含。
问题:
$rootScope
是如何注入此服务的?- 我可以用什么方式重写我的
$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
服务;我想我只需要相信它确实如此。
我遇到了类似的问题,您的 post 帮助我找到了解决方案。我有一个公共库,几乎用于我所有的公共页面活动,例如显示 toast 和管理加载指示器。您的解决方案帮助使用注入器访问该服务。希望这对其他人有帮助!
// custom exception handling
module.config(function ($provide) {
$provide.decorator('$exceptionHandler', ['$delegate', '$injector', function ($delegate, $injector) {
return function (exception, cause) {
$delegate(exception, cause);
var common = $injector.get('common');
common.toast('Whoops, an error occurred!');
common.loading(false);
};
}]);
});