Javascript 如何覆盖 $exceptionHandler 实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13595469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to override $exceptionHandler implementation
提问by dnc253
There are some extra things we want to do anytime a javascript exception is thrown.
每当抛出 javascript 异常时,我们都想做一些额外的事情。
From the docs on $exceptionHandler:
从文档开始$exceptionHandler:
Any uncaught exception in angular expressions is delegated to this service. The default implementation simply delegates to $log.error which logs it into the browser console.
角度表达式中任何未捕获的异常都委托给此服务。默认实现只是委托给 $log.error ,后者将其记录到浏览器控制台中。
The fact that it says "default implementation" makes me think there's a way we can provide our own implementation for the service, and do the stuff we want when an exception is thrown. My question is, how do you do this? How can we keep all exceptions going to this service, but then provide functionality we want to happen?
它说“默认实现”这一事实让我觉得有一种方法可以为服务提供我们自己的实现,并在抛出异常时做我们想做的事情。我的问题是,你如何做到这一点?我们如何让所有异常都进入这个服务,然后提供我们想要的功能?
回答by dnc253
Another option I've found for this is to "decorate" the $exceptionHandlerthrough the $provide.decoratorfunction. This provides you with a reference to the original implementation if you want to use it as part of your custom implementation. So, you can do something like this:
我为此找到的另一个选择是$exceptionHandler通过$provide.decorator函数“装饰” 。如果您想将其用作自定义实现的一部分,这将为您提供对原始实现的引用。所以,你可以做这样的事情:
mod.config(function($provide) {
$provide.decorator("$exceptionHandler", ['$delegate', function($delegate) {
return function(exception, cause) {
$delegate(exception, cause);
alert(exception.message);
};
}]);
});
It will do what the original exception handler does, plus custom functionality.
它将执行原始异常处理程序所做的工作,以及自定义功能。
See this updated fiddle.
看到这个更新的小提琴。
回答by Gloopy
You can override the $exceptionHandlerfunctionality by creating a service with the same name:
您可以$exceptionHandler通过创建具有相同名称的服务来覆盖该功能:
var mod = angular.module('testApp', []);
mod.factory('$exceptionHandler', function () {
return function (exception, cause) {
alert(exception.message);
};
});
See this fiddlefor a sample. If you comment out the factory definition for $exceptionHandleryou will see the error will log to the console instead of alerting it.
有关示例,请参阅此小提琴。如果您注释掉工厂定义,$exceptionHandler您将看到错误将记录到控制台而不是警告它。
Here is a group threadthat has an example of injecting other services like $httpusing $injector.
这是一个组线程,其中包含注入其他服务的示例,例如$http使用$injector.
Note:if you don't want to overwrite the existing functionality of the $exceptionHandler(or another built in service) see this answerfor information on how to decorate a service.
注意:如果您不想覆盖$exceptionHandler(或其他内置服务)的现有功能,请参阅此答案以获取有关如何装饰服务的信息。
回答by Bartek Skwira
You can override any service/factory even $cookieStore.If you want a full-blown, configurable object here is a really nice example:
你可以覆盖任何服务/工厂甚至 $cookieStore。如果你想要一个成熟的、可配置的对象,这里是一个非常好的例子:
var myApp = angular.module('myApp', []);
//provider style, full blown, configurable version
myApp.provider('helloWorld', function() {
this.name = 'Default';
this.$get = function() {
var name = this.name;
return {
sayHello: function() {
return "Hello, " + name + "!"
}
}
};
this.setName = function(name) {
this.name = name;
};
});
//hey, we can configure a provider!
myApp.config(function(helloWorldProvider){
helloWorldProvider.setName('World');
});
function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {
helloWorld.sayHello(),
}

