AngularJs: Revisiting createInjector

In the previous posts we have seen how DI is achieved using $provider and $injector. In this post we go further details of how exactly dependencies are injected into angular. It all starts when module gets loaded. It calls createInjector method with parameter modules to load.

createInjector(modulestoLoad) returns an object called $injector with following methods instantiate, get, has, invoke and annotate. we have gone through details in previous post. Let’s see what exactly each method does in real.

  1. Annotate: Angular must know which dependencies needs to be loaded, annotate will take care of this. It will return an array of dependencies that needs to be injected. Refer below image for more details.Annotate implemenation
  2. Invoke: will call actual implementation of the method by calling javascript’s apply() method by getting all the required informationinvoke implementation
  3. getService: Based on the string it got, it tries to fetch the service from cache, I will talk about cache in next post. If it finds it in cache, it returns the service else instatiate the serivce using factory method, adds it to the cache and returns the service.getservice implementation
  4. Instantiate: It will create a new constructor function for the service and returns the newly created instantiated object.instantiate

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s