AngularJS - Dependency Injection with Examples

Actully Dependency Injection is a software design pattern that implements inversion of control for resolving dependencies.

Let's understand firstly Inversion control This principle what's say
  • High level modules should not depend on low-level modules.Both should depend on abstractions
  • abstraction should not depend on details .Details should depend on abstractions.
  • The interaction between high level and low level modules should be thought of as an abstract interaction between them
It means that objects do not create other objects on which they rely to do their work.Instead of, they get these objects from an outside source.
Instead of, an external source (which in AngularJS, is the AngularJS framework itself) creates the dependent object and gives it to the source object for further usage.


In this article we learn:
Which type of component may be Injected a dependency in Angularjs
Implementation of dependency Injection.

Which type of component may be Injected a dependency in Angularjs
In Angularjs dependencies injected by three methods:
Factory Methods
Module Methods
Controllers

 Factory Methods
Let's understand Factory method define a directive,service or filter is with a factory function. The factory methods are registered with modules.

See following pictorial code which specify factory method.


Module Methods
Let's understand of Module. This method specify functions to run at configuration and run time for a module by calling the config and run methods. These functions are injectable with dependencies just like the factory functions above.


Controllers Methods
Actually Controllers are "classes" or "constructor functions" that are responsible for providing the application behavior that supports the declarative markup in the template. That means the recommended way of declaring Controllers is using the array notation:



Post a Comment

0 Comments