Question about angular.injector() API behavior

56 views
Skip to first unread message

Will Huang

unread,
Aug 30, 2015, 2:18:53 PM8/30/15
to AngularJS
I feel confused about how angular.injector() get $injector component.  As I know, when calling to the angular.injector(['ng', 'app']); will get a new $injector object which is different from the current running Angular app's $injector object.

I made a sample app over here: (Angular 1.3.14)

I'm trying to create a new $injector by using the angular.injector() API.  I'd like to get a brand new $injector in order to get "version" service from getVersion function which is outside of the Angular app.  I was expected that controller shouldn't be run and shouldn't affect my version service object.  I just wanna get the "version" service object only.  In my sample, the value for "version" service has a "current" property and it's value is "0.0.1".  As you can see, there is a controller called "MainCtrl" that will change the version.current value at controller initialization.  When I calling to the getVersion function outside of the Angular app and I created a new $injector object.   In that function, I expected that I should get "0.0.1" but it didn't.  I've got "0.0.2".   I don't know why.  Does anyone know?

Will Huang

unread,
Sep 4, 2015, 5:06:40 PM9/4/15
to AngularJS
Does anyone know about this?  Thanks.

Will Huang於 2015年8月31日星期一 UTC+8上午2時18分53秒寫道:

Sander Elias

unread,
Sep 5, 2015, 12:51:56 AM9/5/15
to AngularJS
Hi Will,

It looks lite that the injector gets the already instantiated version of the module. Have a look at the  variation I made of your pen. That one delivers the 'right' version.  The lack of time/curiosity prevented me from looking over the $injectors source, but if you really want to know, the truth is in there!

Regards
Sander

Will Huang

unread,
Sep 6, 2015, 1:19:30 PM9/6/15
to AngularJS
I traced the Angular code. The loadModules() function looks like load the current running angular module so that although I created a new $injector object, I still get the service object from the current running Angular app.

Sander Elias於 2015年9月5日星期六 UTC+8下午12時51分56秒寫道:

Sander Elias

unread,
Sep 9, 2015, 5:54:12 AM9/9/15
to AngularJS
Hi Will,

That confirms my suspicion. If an app is already instantiated, the injector will get that, in stead of creating an new instance. Makes a lot of sense, but it's not what you where hoping for. Your goal is to be able to detect versions of parts of your app if I read your question right?

Regards
Sander 

Will Huang

unread,
Sep 9, 2015, 5:58:32 AM9/9/15
to AngularJS
Hi Sander,

Well, I'm not hoping for that.  I just want to confirm what's the exact behavior about how angular.injector() get $injector instance because the document is not saying much about this detail.  Actually, every time you call angular.injector(), you are truly get a new $injector instance, but this injector will get the service from the instanced modules if the app has been instantiated.


Sander Elias於 2015年9月9日星期三 UTC+8下午5時54分12秒寫道:

Sander Elias

unread,
Sep 9, 2015, 6:02:10 AM9/9/15
to AngularJS
Hi Will,

Well, then I can confirm that's indeed the expected behavior. At least in the NG1.x branch. It's the result of how the NG1 injecttion system works.

Regards
Sander 
Reply all
Reply to author
Forward
0 new messages