[英]Using Angularjs-Toaster inside the decorator of $exceptionHandler
I am trying to toast errors those are handled in the $exceptionHandler decorator as follows, 我试图在$ exceptionHandler装饰器中处理错误,如下所示,
var app = angular.module('myApp',['toaster']);
app.config(function($provide){
$provide.decorator('$exceptionHandler',function($delegate,toaster){
toaster.pop('error','text','error');
$delegate(exception, cause);
});
});
Here is the plunkr. 这是傻瓜。 This is giving me the following error,
这给了我以下错误,
Error: [$injector:cdep] Circular dependency found: $rootScope <- toaster <- $exceptionHandler <- $rootScope
I am using AngularJS-Toaster for showing errors. 我正在使用AngularJS-Toaster来显示错误。 How can I inject toaster service inside the decorator now?
我现在如何在装饰器内注入烤面包机服务?
You can inject the $injector
into your decorator and wrap your injection in a function. 您可以将
$injector
注入装饰器并将注入包装在函数中。 This delays the injection of the toaster service until you invoke the $exceptionHandler
, preventing a circular dependency. 这会延迟多士服务的注入,直到您调用
$exceptionHandler
,从而防止出现循环依赖。
var app = angular.module('myApp',['toaster']);
app.config(function($provide){
$provide.decorator('$exceptionHandler',function($delegate,$injector){
return function (exception, cause) {
var toaster = $injector.get('toaster');
toaster.pop('error','text','error');
}
});
});
To expand on why this is a circular dependency, you have to look at what is being required by both the injected service, as well as what is occurring in the decorator. 为了扩大为什么这是一个循环依赖,你要看看被需要的是双方注入的服务,以及什么是装饰发生。
The toaster
service has a dependency on $rootScope
and is being injected into the decorator for for $exceptionHandler
. toaster
服务依赖于$rootScope
,并被注入装饰器以获取$exceptionHandler
。 However, $rootScope
in turn has a dependency on $exceptionHandler
. 但是,
$rootScope
反过来依赖于$exceptionHandler
。 This ends up creating a circular reference. 这最终会创建一个循环引用。
You would find the same behavior if $http
or $q
was injected instead of toaster
since they also have dependencies on $rootScope
. 如果
$http
或$q
被注入而不是toaster
你会发现相同的行为,因为它们也依赖于$rootScope
。 it isn't toaster
specifically that is the issue...rather it is the $rootScope
dependency while trying to also apply behavior to a dependency of $rootScope
. 它不是
toaster
明确这是问题...而它是$rootScope
依赖,而试图也适用行为的依赖$rootScope
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.