简体   繁体   English

如何处理AngularJS配置块错误

[英]How to handle AngularJS config block errors

Hi folks I am new to angular js. 嗨,大家好,我是angular js的新手。 I have a module 'ngApp' which depends on a module 'ng-main'. 我有一个模块“ ngApp”,它依赖于模块“ ng-main”。 I want to handle errors and exceptions that might be thrown in a config block of 'ng-main' module. 我想处理可能在“ ng-main”模块的配置块中引发的错误和异常。 I have created a new module 'ng-error' and made 'ngApp' dependent on this module to handle errors and exceptions but it's kind of not catching it. 我创建了一个新模块'ng-error',并使'ngApp'依赖于此模块来处理错误和异常,但这种方式无法捕获。

index.html 的index.html

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

ngErrors.js 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 ngMain.js

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

If any errors occurs in this config block it totally fails but I want to manually handle those. 如果此配置块中发生任何错误,则它完全会失败,但我想手动处理这些错误。 Any thoughts or comments on my code? 对我的代码有任何想法或意见吗?

Okay, Tried below code and worked. 好的,尝试了以下代码并开始工作。 Manually overridden angular bootstrap process and wrapping that code inside try{} catch {} block to handle errors in config block 手动覆盖角度引导程序并将该代码包装在try {} catch {}块中以处理配置块中的错误

something like this: 像这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM