现在我的任务是重写$exceptionHandler提供程序,以便它可以输出带有消息和停止默认事件的模式对话框。
我的工作是:
在项目初始化中,我使用了.provider方法:
.provider('$exceptionHandler', function(){
//and here I would like to have rootScope to make event broadcast
})
标准注入方法不起作用。
发布于 2013-02-20 00:19:23
您可以注入注入器并查找$rootScope。
Demo plunkr:http://plnkr.co/edit/0hpTkXx5WkvKN3Wn5EmY?p=preview
myApp.factory('$exceptionHandler',function($injector){
return function(exception, cause){
var rScope = $injector.get('$rootScope');
if(rScope){
rScope.$broadcast('exception',exception, cause);
}
};
})
更新:也添加了.provider技术:
app.provider('$exceptionHandler', function() {
// In the provider function, you cannot inject any
// service or factory. This can only be done at the
// "$get" method.
this.$get = function($injector) {
return function(exception,cause){
var rScope = $injector.get('$rootScope');
rScope.$broadcast('exception',exception, cause);
}
};
});
发布于 2013-11-02 20:33:15
我这样做的方式--使用装饰器,并在未知错误时恢复到以前的异常处理程序:
app.config(function ($provide) {
$provide.decorator('$exceptionHandler', function($delegate, $injector) {
return function (exception, cause) {
if (ICanHandleThisError) {
var rootScope= $injector.get('$rootScope');
// do something (can use rootScope)
} else
$delegate(exception, cause);
};
});
});
发布于 2013-02-19 18:08:34
您需要注入$rootScope:
.provider('$exceptionHandler', '$rootScope', function(){
//and here I would like to have rootScope to make event broadcast
})
这是你试过的吗?如果是这样,您是否有错误消息或jsfillde/plnkr来查看它失败的原因?
https://stackoverflow.com/questions/14954811
复制相似问题