Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular JS:$exceptionHandler没有try或throw块?_Angularjs_Try Catch_Exceptionhandler - Fatal编程技术网

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);