现在我的任务是重写$ exceptionHandler提供程序,以便它将输出带有消息的模态对话框并停止默认事件。
我的所作所为:
项目init中的我使用方法.provider:
.provider('$exceptionHandler', function(){
//and here I would like to have rootScope to make event broadcast
})
标准注射方法不起作用。
UPD :沙箱 - http://jsfiddle.net/STEVER/PYpdM/
答案 0 :(得分:31)
您可以注入进样器并查找$ 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);
}
};
});
答案 1 :(得分:1)
我这样做 - 使用装饰器并在未知错误上恢复到先前的异常处理程序:
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);
};
});
});
答案 2 :(得分:-2)
你需要注入$ rootScope:
.provider('$exceptionHandler', '$rootScope', function(){
//and here I would like to have rootScope to make event broadcast
})
这是你试过的吗?如果是这样,你有错误信息或jsfillde / plnkr,看看它失败的原因?