AngularJS Dependency Injection
AngularJS Dependency Injection
What is Dependency Injection
The explanation on the wiki is: Dependency Injection (DI) is a software design pattern in which one or more dependencies (or services) are injected (or passed by reference) into an independent object (or client), and then become part of that client's state.
This pattern separates the creation of the client's dependencies' behavior, which makes the program design loosely coupled and adheres to the Dependency Inversion and Single Responsibility principles. In direct contrast to the Service Locator pattern, it allows the client to know how the client uses the system to find dependencies.
One sentence --- Don't come looking for me, I will come to you if there is something.
AngularJS provides a good dependency injection mechanism. The following 5 core components are used for dependency injection:
- value
- factory
- service
- provider
- constant
value
Value is a simple javascript object used to pass values to the controller (configuration phase):
Original prompt (Chinese, left untranslated)
// Define a module
var mainApp = angular.module("mainApp", []);
// Create value object "defaultInput" and pass data
mainApp.value("defaultInput", 5);
...
// Inject "defaultInput" 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);
}
});
factory
factory is a function used to return a value. It is created when needed by the service and controller.
Usually, we use factory functions to calculate or return values.
Original prompt (Chinese, left untranslated)
// Define a module
var mainApp = angular.module("mainApp", []);
// Create factory "MathService" for the product of two numbers provides a method multiply to return multiplication of two numbers
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
// Inject factory "MathService" in service
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
...
provider
In AngularJS, a service, factory, etc., is created through a provider (configuration phase).
The Provider provides a factory method get(), which is used to return a value/service/factory.
Original prompt (Chinese, left untranslated)
// Define a module
var mainApp = angular.module("mainApp", []);
...
// Use provider to create a service and define a method to calculate the product of two numbers
mainApp.config(function($provide) {
$provide.provider('MathService', function() {
this.$get = function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
};
});
});
constant
constant is used to pass values during the configuration phase; note that this constant is unavailable during the configuration phase.
mainApp.constant("configParam", "constant value");
instance
The following instances demonstrate the above dependency injection mechanisms.
AngularJS instance - factory
var
mainApp
=
angular
.
module
(
"
mainApp
"
,
[
]
)
;
mainApp
.
value
(
"
defaultInput
"
,
5
)
;
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
)
;
}
}
)
;
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
)
;
}
}
)
;
Try it »
AngularJS instance - provider
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
"
,
5
)
;
mainApp
.
service
(
'
CalcService
'
,
function
(
MathService
)
{
this
.
square
=
function
(
a
)
{
return
MathService
.
multiply
(
a
,
a
)
;
}
}
)
;
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
)
;
}
}
)
;
Try it »