The problem is in the flow of execution of Angular's code. To integrate on @pkozlowski.opensource's answer and related comments, note that the $injector
property is made available to DOM elements after the module code is executed, so that when you access the property (as when you try to console.log
it) the property will still be undefined
.
You can solve this problem by simply setting a 0 timeout to the execution of the logging function (i.e., no explicit delay in milliseconds is needed). This hack works because the logging function will be executed when the stack is empty, that is when Angular's bootstrapping has finished and the $injector
property is available to the DOM elements.
Another (maybe better) solution in Angular's lingo would be to include your $injector
-accessing code in a run block (see also the related API). The $injector
could then be easily injected into the initialization function as a normal service. This works because run blocks are queued and executed asynchronously when all bootstrapping has terminated.
Below you can find two fiddles, one for each solution.
0 Timeout jsFiddle
Run block jsFiddle
EDIT
Also, there is usually no need to explicitly load the ng
module. In normal cases, the ng
model is already automatically loaded, unless you want to perform a manual bootstrap of Angular's code. The piece of docs referenced in one of the answers refers (implicitly, unfortunately) to Angular's manual bootstrapping procedure, when you have to manually create the injector and tell it which modules you want to load