[英]Angular lifecycle: How do I execute a javascript function AFTER Angular has been initialized?
[英]How can I detect if an Angular app has been initialized from outside Angular?
尝试这样的事情:
try {
angular.bootstrap(document)
}
catch(e) {
console.log(!!e.message.indexOf('btstrpd'))
//will log true if the error is of the type `btstrpd`,
//you can do whatever you want with it here.
}
如果已经有一个app bootstrap, angular.bootstrap
将发出错误。 btstrpd
是错误的名称,表示应用程序已经被引导。
这很简单,我希望它适用于你的情况,但是如果它没有ping我,我会想到更精细的东西。
另一种方法:
如果您有关于模块的一些信息,可以使用以下方法检查组件是否已经过引导:
angular.element(document.querySelector('[ng-app]')).injector().has('$http')
这个发现其中ng-app
中使用属性,然后调用初始化喷油器的实例,从中你可以叫has
看到什么提供商已被初始化。
另一种方法:
您可以尝试使用angular.module
而不使用第二个参数,而不是angular.bootstrap
,如果模块已加载或发出错误,则应检索模块。
try {
angular.module('moduleName')
}
catch(e) {
console.log(!!e.message.indexOf('nomod'))
//will log true if the error is of the type `nomod`,
//you can do whatever you want with it here.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.