Dependency injection is a software design pattern that states that instead of creating an instance of a dependent service or object when we need, the class or function should ask for it.
The Angular injector system is responsible to create the component, services, and resolve dependencies of those services or objects and provide to the calling class or functions.
// No dependency injection
function GetData() {
var $http = new HttpService();
return $http.get("/Article/getData");
}
// Dependency injection
function GetData($http) {
return $http.get("/Article/getData");
}
AngularJS guarantees only one instance of the dependent object and all other dependent function gets the same instance of the object. This is done using Singleton design pattern and the same instance remains available in the entire scope of the application (in the whole web page). It means that two or more controllers or services or function asking for this $http service gets the same instance of the object.
Views: 11468 | Post Order: 14