我编写了一个工厂来覆盖内置的 $exceptionHandler,因为我计划创建一个可以从前端登录的后端服务。但现在,我只想捕获异常,将其记录到控制台,然后抛出它,因为知道 console.log
将被服务调用替换。
angular.module('exceptionOverride', []).factory('$exceptionHandler', function() {
return function(exception) {
console.log(exception);
throw exception;
};
});
在我的 Controller 的 init 函数中,我放入了
var x = n + 1
因此,由于 n
未定义,它会抛出错误。
但是,在 Controller 初始化时,会发生这种情况:
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:6 Uncaught ReferenceError: n is not definedinit @ taxAccountNameController.js:17taxAccountNameController @ taxAccountNameController.js:12invoke @ angular.js:4478instantiate @ angular.js:4486(anonymous function) @ angular.js:9151$interpolate.compile @ angular-ui-router.js:4018invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555updateView @ angular-ui-router.js:3959directive.compile @ angular-ui-router.js:3927invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555$interpolate.compile @ angular-ui-router.js:4026invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555updateView @ angular-ui-router.js:3959(anonymous function) @ angular-ui-router.js:3921parent.$get.Scope.$broadcast @ angular.js:16311$state.transitionTo.$state.transition.resolved.then.$state.transition @ angular-ui-router.js:3311processQueue @ angular.js:14745(anonymous function) @ angular.js:14761parent.$get.Scope.$eval @ angular.js:15989parent.$get.Scope.$digest @ angular.js:15800parent.$get.Scope.$apply @ angular.js:16097done @ angular.js:10546completeRequest @ angular.js:10744requestLoaded @ angular.js:10685
angular.js:68 Uncaught Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.7/$rootScope/inprog?p0=%24digestREGEX_STRING_REGEXP @ angular.js:68beginPhase @ angular.js:16346parent.$get.Scope.$digest @ angular.js:15780(anonymous function) @ angular.js:16028completeOutstandingRequest @ angular.js:5507(anonymous function) @ angular.js:5784
我的 exceptionOverride
中的 console.log 触发了 7 次,然后抛出了异常(如预期),但随后我也收到了 $digest 已在进行中
错误。
我做错了什么?
最佳答案
由于您覆盖了 Angular 的 native $exceptionHandler
,因此它无法完成其工作。 $exceptionHandler
包含的行不仅仅是 console.log
和 throw
。它有一些代码可以暂停其他 Angular 服务或提供程序以避免此类事情。
关于javascript - 当尝试覆盖 $exceptionHandler 时,简单的 javascript 异常在 Angular Controller init 上发生 7 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33017476/