What is dependency injection in AngularJS?

There is a general term "Inversion of Control" of which "Dependency Injection" is a sub-term. IoC means instead of taking away control flow to a new object when needed in decoupled objects, but to pass the alert or data to objects to perform the job or task on their own. You can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways.

DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external mean e.g. constructor injection, setter injection or method injection. Finally, Dependency injection is the practice of passing in the object’s collaborators (the code objects that our object depends on) into it, rather than creating them inside the object.

AngularJS is said to be the most unit test friendly framework due to its modular nature. Even you can separate the big parts of your application to different modules. For example a Product_Module and a Auth_Module. Within those modules you can have repetitive tasks in services being used in controllers. There are also built-in services for some necessary operations like server-client communication and routing. Now using these services or modules where your code depends on those some methods of services is called Dependency Injection.

Below is $http being injected to be used in an AngularJS Controller.

1. function controller($http){
2.           $http.get("/url").success(function(data)
3.                                            {
4.                                                  console.log(data);
5.                                            }
6.                                  );
7. }

Answer by Ahmad Ali:

There is a general term "Inversion of Control" of which "Dependency Injection" is a sub-term. IoC means instead of taking away control flow to a new object when needed in decoupled objects, but to pass the alert or data to objects to perform the job or task on their own. You can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways.

DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external mean e.g. constructor injection, setter injection or method injection. Finally, Dependency injection is the practice of passing in the object’s collaborators (the code objects that our object depends on) into it, rather than creating them inside the object.

AngularJS is said to be the most unit test friendly framework due to its modular nature. Even you can separate the big parts of your application to different modules. For example a Product_Module and a Auth_Module. Within those modules you can have repetitive tasks in services being used in controllers. There are also built-in services for some necessary operations like server-client communication and routing. Now using these services or modules where your code depends on those some methods of services is called Dependency Injection.

Below is $http being injected to be used in an AngularJS Controller.

function controller($http){
          $http.get("/url").success(function(data)
                                           {
                                                 console.log(data);
                                           }
                                 );
}

What is dependency injection in AngularJS?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s