19. What is dependency injection and what are the benefits of it?

Dependency injection is a powerful design pattern that allows separating the concerns of different components in an application and provides a way to inject the dependent component into the client component.

Consider the below code:

myApp.controller(‘myController’, function ($scope, $http, $location)
{
//logic
});
Here, a controller is declared with its dependencies.

.$http, $location are all services which are injected into the controller as a dependent entity. All of them have some independent specific task associated with it. MyController does not need to create their instance, but it can directly use them.

Leave a Reply