angularjs - angularjs处理异常的方法

我有一个 Angular 应用程序。它有 n 个 Controller 。我知道 $exceptionHandler。这是自定义异常处理程序。

'use strict';

angular.module('exceptionHandlingApp')
.config(function($provide) {
$provide.decorator('$exceptionHandler', ['$log', '$delegate',
  function($log, $delegate) {
    return function(exception, cause) {
      $log.debug('Default exception handler.');
      $delegate(exception, cause);
    };
  }
]);
});

Controller

 app.controller('cust1Controller',function($scope,$exceptionHandler)
 {
    //ajax request or anything
 }

 app.controller('cust2Controller',function($scope,$exceptionHandler)
 {
    //ajax request or anything
 }

如果任何 Controller 发生异常,我该如何调用我的自定义处理程序?

最佳答案

您不应该调用异常处理程序。它由 AngularJS 自动调用。

为了确保,您可以使用 debugger 关键字。让我举个例子。

angular
    .module("app")
    .config(logExceptionToAPIServer);

function logExceptionToAPIServer($provide) {
    $provide.decorator("$exceptionHandler", ["$delegate",
        function ($delegate) {
            return function (exception, cause) {
                debugger;
                var http = angular.injector(['ng']).get('$http');
                debugger;
                var request = {
                    Message: exception.message,
                    StackTrace: exception.stack
                };
                http.post('yourAddress//', request);
                $delegate(exception, cause);
            };
        }
    ]);
};

https://stackoverflow.com/questions/37322500/

相关文章:

android - 调用 overridePendingTransiton 时应用程序卡住

msbuild - 在自定义目标中计算的编译器附加选项

android - 如何杀死 fragment ?

c# - WCF:没有 channel 主动监听

jenkins - 在非交互式 shell 中运行 docker 命令

spring - Spring 中的 RequestMapping 包

php - Laravel 自定义分页

c# - 标准化相对路径?

macos - XAMPP 错误 - #1932 - 引擎中不存在表 '---'

javascript - 使用在 Safari 中不起作用的 javascript 禁用键盘快捷键?