gpt4 book ai didi

angularjs - angularjs处理异常的方法

转载 作者:行者123 更新时间:2023-12-05 07:49:30 25 4
gpt4 key购买 nike

我有一个 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);
};
}
]);
};

关于angularjs - angularjs处理异常的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com