AngularJS 依赖注入
依赖注入是一种软件设计方式,组件在其中被赋予它们的依赖项,而不是在组件内部进行硬编码。它使组件从查找依赖关系中解脱出来,使依赖关系可配置。它还有助于使组件可复用、可维护和可测试。
AngularJS提供了一个出色的依赖注入机制。它提供以下核心组件,可以作为依赖项注入到彼此中:
- Value
- Factory
- Service
- Provider
- Constant
Value
Value是一个简单的JavaScript对象,在配置阶段(AngularJS自启动阶段)期间需要将值传递给控制器。
//define a module
var mainApp = 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
Factory是一个用来返回值的函数。它在服务或控制器需要时按需创建一个值。通常使用一个工厂函数来计算并返回该值。
//define a module
var mainApp = angular.module("mainApp", []);
//create a factory "MathService" which 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 the factory "MathService" in a service to utilize the multiply method of factory.
mainApp.service('CalcService', function(MathService) {
this.square = function(a) {
return MathService.multiply(a,a);
}
});
...
Service
Service是一个单例的JavaScript对象,包含一组执行特定任务的函数。服务使用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
在配置阶段,AngularJS内部使用提供者来创建服务、工厂等。以下脚本可用于创建之前创建的MathService。提供者是一个特殊的工厂方法,带有get()方法,用于返回值/服务/工厂。
//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;
};
});
});
Constant
Constant在配置阶段使用,考虑到在配置阶段不能使用值。
mainApp.constant("configParam", "constant value");
示例
下面的示例展示了所有上述指令的使用方法:
testAngularJS.htm
<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" /></p>
<button ng-click = "square()">X<sup>2</sup></button>
<p>Result: {{result}}</p>
</div>
<script src = "https://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", 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);
}
});
</script>
</body>
</html>
输出
在Web浏览器中打开 testAngularJS.htm 并查看结果。