Dependency injection is the act of supplying values or object instances (dependencies) to target objects from outside of the target object. In many (most?) cases this is automated by a framework, such as AngularJS.
This means that a given target object does not create its own dependencies, through the use of thenew keyword or other creation methods.
By creating and managing dependencies outside of an object, it makes it much easier to switch out that dependency as needed. This is very useful when you are writing your unit tests, and can have many advantages in larger systems.
There are only three ways for an object to resolve its dependencies:
- internally, via the new operator
- lookup via a global variable (requirejs is an example)
- the dependency is passed to the object
The third option is dependency injection, and it is the preferred approach in AngularJS apps.
Your new post is loading...