joe*_*cii 3 javascript angularjs angularjs-scope
在开发Lift-ng插件时,我注意到一个问题,有时服务器会在角度初始化之前通过 Comet 向客户端发送事件。这会导致应用程序默默地错过事件。我想知道是否有办法检测应用程序是否已初始化,即所有控制器、服务、指令等都已实例化,因此所有侦听器都已为事件做好准备。因为这是一个插件,所以我需要能够在不需要 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.
}
Run Code Online (Sandbox Code Playgroud)
如果已经有一个应用程序启动,angular.bootstrap将发出错误。btstrpd是错误的名称,表示应用程序已经被引导。
这很简单,我希望它适用于您的情况,但如果它没有 ping 我,我会考虑更详细的内容。
另一种方法:
如果您有关于模块的一些信息,您可以使用以下命令检查组件是否已引导:
angular.element(document.querySelector('[ng-app]')).injector().has('$http')
这会找到ng-app属性的使用位置,然后调用初始化的注入器实例,您可以从中调用has以查看哪些提供程序已被初始化。
还有一种方法:
而不是angular.bootstrap您可以尝试angular.module不使用第二个参数,如果模块已加载或发出错误,它应该检索模块。
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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4400 次 |
| 最近记录: |