While developing the lift-ng plugin, I have noticed an issue where sometimes the server sends events via comet to the client prior to angular initialization. This causes events to be silently missed by the application. I would like to know if there is a way to detect if an app has been initialized, i.e. all controllers, services, directives, etc have been instantiated and hence all listeners are ready for events. Because this is a plugin, I need to be able to do it without requiring the angular components to implement any code. I can add a controller, service, or whatever to the app, but the solution cannot require that every component sends an event, for instance.
-
maybe this helps: http://stackoverflow.com/questions/10490570/call-angular-js-from-legacy-code – Jorg Jul 01 '14 at 00:42
-
u know that `angular.js` is javascript right? javascript. – Ryan Jul 01 '14 at 00:57
-
1@true Yeah... I tagged it as JS. – joescii Jul 01 '14 at 01:32
-
@Jorg thanks for the tip. That is precisely how I get those events to cross over into the angular world. The problem is they can arrive before the app is initialized and the events are dropped. – joescii Jul 01 '14 at 01:43
1 Answers
Try something like this:
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.
}
If there is already an app bootstrapped, angular.bootstrap
will emit an error. btstrpd
is the name of the error that means an app has already been bootstrapped.
This is simple enough, and I hope it works for your case, but if it doesn't ping me and I'll think of something more elaborate.
Another approach:
If you have some information about the modules, you can check the components have been bootstrapped using this:
angular.element(document.querySelector('[ng-app]')).injector().has('$http')
This finds where the ng-app
attribute was used, then calls the initialized injector instance, from which you can call has
to see what providers have been initialized.
Yet another approach:
Instead of angular.bootstrap
you can try angular.module
without a second argument, which should retrieve the module if it has been loaded or emit an error.
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.
}

- 7,099
- 3
- 34
- 51
-
Sounds interesting. However, this could interfere with someone else's bootstrapping, right? For instance, if the page has multiple apps which requires some special-handling. Thanks for taking interest! – joescii Jul 01 '14 at 01:35
-
-
Doesn't seem to work. If my event calls `bootstrap` first, then the application fails to boot because it has already been bootstrapped on that element. – joescii Jul 01 '14 at 02:21
-
-
Nice. This gives me another idea. Perhaps it is sufficient to check if `scope()` returns a defined object. Due to the single-threaded spec of a browser, I assume that if that object returns then the whole app has been initialized. – joescii Jul 01 '14 at 14:50
-
@joescii well, it means something has been initialized. Could be just angular without modules too. If it works for you, that's great. Consider accepting the answer :) – Mosho Jul 01 '14 at 17:58
-
Based on some preliminary investigation, this approach I mentioned should work. Thanks for setting me on the right path! – joescii Jul 02 '14 at 01:55