AngularJS Dependency Injection
AngularJS Dependency Injection
AngularJS Dependency Injection
Dependency Injection is a software design pattern that specifies how components get
holds of their dependencies. In this pattern, components are given their
dependencies instead of coding them within the component.
Modularizing your application makes it easier to reuse, configure and test the
components in your application. Following are the core types of objects and
components:
value
factory
service
provider
constant
These objects and components can be injected into each other using AngularJS
Dependency Injection.
Value
//define a module
var myModule = angular.module("myModule", []);
//create a value object and pass it a data.
myModule.value("numberValue", 100);
myModule.value("stringValue", "abc");
myModule.value("objectValue", { val1 : 123, val2 : "abc"} );
Here, values are defined using the value() function on the module. The first
parameter specifies the name of the value, and the second parameter is the value
itself. Factories, services and controllers can now reference these values by their
name.
Injecting a value
To inject a value into AngularJS controller function, add a parameter with the same
when the value is defined.
Let's take an example that defines a factory on a module, and a controller which
gets the factory created value injected:
To inject a value into AngularJS controller function, add a parameter with the same
when the value is defined.
Service
//define a module
var mainApp = angular.module("mainApp", []);
...
//create a service which defines a method square to return square of a number.
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
//inject the service "CalcService" into the controller
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
$scope.number = defaultInput;
$scope.result = CalcService.square($scope.number);
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
Provider
//define a module
var mainApp = angular.module("mainApp", []);
...
//create a service using provider which defines a method square to return square of
a number.
mainApp.config(function($provide) {
$provide.provider('MathService', function() {
this.$get = function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
};
});
});
Constants
You cannot inject values into the module.config() function. Instead constants are
used to pass values at config phase.
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Dependency Injection</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<script src =
"http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.config(function($provide) {
$provide.provider('MathService', function() {
this.$get = function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
};
});
});
mainApp.value("defaultInput", 10);
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
});
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
</script>
</body>
</html>