This article mainly introduces the dependency injection mechanism in the detailed angularjs, which plays a very important role in the use of the JavaScript components, and the friends who need it can refer to
Dependency Injection is a software design pattern given in a component that replaces the dependencies of a hard component to encode them. This eases one component, from locating dependencies, to relying on configuration. This helps to make components reusable, maintainable, and tested.
Angularjs provides a paramount dependency injection mechanism. It provides one of the following core components that can be injected to each other.
Value
Factory
Service
Provided by
Constant value
Value
A value is a simple JavaScript object that is used to transfer the value of a configuration phase controller in the process.
//define a module
varmainApp = angular.module("mainApp", []);
//create a value object as "defaultInput" and pass it a data.
mainApp.value("defaultInput", 5);
...
//inject the value in the controller using its name "defaultInput"
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
The factory is the value that is used to return the function. It creates values based on requirements whenever a service or controller is needed. It usually uses a factory function to compute and return the corresponding value
//define a module
varmainApp = angular.module("mainApp", []);
//create a factory "MathService" which provides a method multiply to return multiplication of two numbers
mainApp.factory('MathService',function() {
varfactory = {};
factory.multiply =function(a, b) {
returna * b
}
returnfactory;
});
//inject the factory "MathService" in a service to utilize the multiply method of factory.
mainApp.service('CalcService',function(MathService){
this.square =function(a) {
returnMathService.multiply(a,a);
}
});
...
Service
A service is a single JavaScript that contains a set of function objects to perform certain tasks. Services use the service () function, which is then injected into the definition of the controller.
//define a module
varmainApp = 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) {
returnMathService.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);
}
});
Provided by
The provider uses the ANGULARJS internal creation process during the configuration phase of the service, the factory, and so on (phase angularjs to boot itself). The script mentioned below can be used to create the mathservice that we have created before. The provider is a special factory method and a Get () method to return a value/service/factory.
//define a module
varmainApp = 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() {
varfactory = {};
factory.multiply =function(a, b) {
returna * b;
}
returnfactory;
};
});
});
Constant
Constants are used to consider facts by configuring the phase value, and the value cannot be passed during the configuration phase of the period.
|
Mainapp.constant ("Configparam", "constant value"); |
Example
The following example shows all of the above instructions.
Testangularjs.html
<html>
<head>
<title>AngularJS Dependency Injection</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp"ng-controller="CalcController">
<p>Enter a number: <input type="number"ng-model="number"/>
<button ng-click="square()">X<sup>2</sup></button>
<p>Result: {{result}}</p>
</div>
<script>
varmainApp = angular.module("mainApp", []);
mainApp.config(function($provide) {
$provide.provider('MathService',function() {
this.$get =function() {
varfactory = {};
factory.multiply =function(a, b) {
returna * b;
}
returnfactory;
};
});
});
mainApp.value("defaultInput", 5);
mainApp.factory('MathService',function() {
varfactory = {};
factory.multiply =function(a, b) {
returna * b;
}
returnfactory;
});
mainApp.service('CalcService',function(MathService){
this.square =function(a) {
returnMathService.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);
}
});
</script>
</body>
</html>
Results
Open textangularjs.html in a Web browser. See the results below.