Angularjs Angular JS:$exceptionHandler没有try或throw块?
是否可以使用Angularjs Angular JS:$exceptionHandler没有try或throw块?,angularjs,try-catch,exceptionhandler,Angularjs,Try Catch,Exceptionhandler,是否可以使用$exceptionHandler全局处理angularJs中的所有异常,而无需写入try或throw块 我想要的是,即使我忘记为vara=1/0之类的语句编写try-catch块,我也希望在上面的代码中处理它。是的,在AngularJS中可以进行全局错误处理。基本上,在配置时,您可以修改服务的默认行为。 代码如下所示: app.factory('$exceptionHandler', function() { return function(exception, ca
$exceptionHandler
全局处理angularJs中的所有异常,而无需写入try
或throw
块
我想要的是,即使我忘记为var
a=1/0
之类的语句编写try-catch
块,我也希望在上面的代码中处理它。是的,在AngularJS中可以进行全局错误处理。基本上,在配置时,您可以修改服务的默认行为。
代码如下所示:
app.factory('$exceptionHandler', function() {
return function(exception, cause) {
exception.message += ' (caused by "' + cause + '")';
throw exception;
};
});
注意:在某些情况下,您还应该调用$delegate
,因为它是原始服务实例。在本例中,请看一下,它只执行以下操作:
angular
.module('global-exception-handler', [])
.config(['$provide', function($provide) {
$provide
.decorator('$exceptionHandler', ['$delegate', function($delegate) {
return function(exception, cause) {
$delegate(exception, cause);
// Do something here
};
}]);
}]);
来源:
$log.error.apply($log, arguments);