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.
- 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.
- 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.
- Instantiate: It will create a new constructor function for the service and returns the newly created instantiated object.