依赖注入是一种软件设计,其中为组件提供了相关性,而不是在组件中对其进行硬编码。它使组件不必查找依赖关系,并使依赖关系可配置。它还有助于使组件可重用,可维护和可测试。
AngularJS提供了一种最高的依赖注入机制。它提供了以下核心组件,它们可以作为依赖项相互注入。
Value
Factory
Service
Provider
Constant
Value - 值
Value 是一个简单的JavaScript对象,在配置阶段(配置阶段是AngularJS引导自身时),需要将值传递给控制器。
//定义模块
var mainApp = angular.module("mainApp", []);
//创建一个值对象作为“ defaultInput”并向其传递数据。"defaultInput" and pass it a data.
mainApp.value("defaultInput", 5);
...
//使用名称“ defaultInput”将值注入控制器"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 是用于返回值的函数。每当服务或控制器需要时,它都会按需创建值。它通常使用Factory函数来计算并返回值。
//定义模块
var mainApp = angular.module("mainApp", []);
//创建一个工厂“MathService”,它提供一个乘法方法来返回两个数字的乘法
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b
}
return factory;
});
//在服务中注入工厂“MathService”以利用factory的乘法方法。
mainApp.service('CalcService', function(MathService) {
this.square = function(a) {
return MathService.multiply(a,a);
}
});
...
Service - 服务
服务(Service)是一个单例JavaScript对象,其中包含一组执行某些任务的功能。使用service()
方法定义服务,然后将其注入到控制器中。
//定义模块
var mainApp = angular.module("mainApp", []);
...
//创建一个服务,该服务定义方法平方以返回数字的平方。
mainApp.service('CalcService', function(MathService) {
this.square = function(a) {
return MathService.multiply(a,a);
}
});
//将服务“ CalcService”注入控制器"CalcService"
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。Provider是一种特殊的工厂方法,其get()
方法用于返回value/service/factory。
//定义模块
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;
};
});
});
Constant - 常量
考虑到在配置阶段无法使用值的事实,常量用于在配置阶段传递值。
mainApp.constant("configParam", "constant value");
示例
以下示例显示了所有上述指令的使用-
testAngularJS.htm
<html>
<head>
<title>AngularJS Dependency Injection</title>
</head>
<body>
<h2>AngularJS-依赖注入应用示例</h2>
<div ng-app = "mainApp" ng-controller = "CalcController">
<p>输入一个数字: <input type = "number" ng-model = "number" /></p>
<button ng-click = "square()">X<sup>2</sup></button>
<p>结果: {{result}}</p>
</div>
<script src = "https://cdn.staticfile.org/angular.js/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>
也可以在网络浏览器中打开testAngularJS.htm并查看结果。