如何处理 AngularJS 配置块错误

How to handle AngularJS config block errors

大家好,我是 angular js 的新手。我有一个模块 'ngApp',它依赖于一个模块 'ng-main'。我想处理可能在 'ng-main' 模块的配置块中抛出的错误和异常。我创建了一个新模块 'ng-error' 并使 'ngApp' 依赖于此模块来处理错误和异常,但它有点没有捕捉到它。

index.html

<script type="text/javascript">
  angular.module('ngApp', ['ng-error', 'ng-main']);
</script> 

ngErrors.js

angular.module('ng-error',[])
 .config(function($provide) {
    $provide.factory('$exceptionHandler', function($injector) {
        return function (exception, cause) {
            console.log(exception);

            // display error message 

        };
    });
});

ngMain.js

angular.module('ng-main', ['ng-test'])
.config(function(){
  // code with error
}); 

如果此配置块中出现任何错误,它将完全失败,但我想手动处理这些错误。对我的代码有什么想法或意见吗?

好的,尝试了下面的代码并成功了。手动覆盖 angular bootstrap 处理并将该代码包装在 try{} catch {} 块中以处理配置块中的错误

像这样:

  <script type="text/javascript">
    angular.module('ngApp', ['ng-error', 'ng-main']);

   angular.element(document).ready(function() {
     try {
        angular.bootstrap(document, ['ngApp']);
     } catch(e) {
       //handle errors
     }
  });
  </script>