欢迎您访问我爱IT技术网,今天小编为你分享的javascript教程:【详解AngularJS中的依赖注入机制】,下面是详细的讲解!
详解AngularJS中的依赖注入机制
依赖注入是一个在组件中给出的替代了硬的组件内的编码它们的依赖关系的软件设计模式。这减轻一个组成部分,从定位的依赖,依赖配置。这有助于使组件可重用,维护和测试。
AngularJS提供了一个至高无上的依赖注入机制。它提供了一个可注入彼此依赖下列核心组件。
- 值
- 工厂
- 服务
- 提供者
- 常值
值
值是简单的JavaScript对象,它是用来将值传递过程中的配置相位控制器。
//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);
}
});
工厂
工厂是用于返回函数的值。它根据需求创造值,每当一个服务或控制器需要。它通常使用一个工厂函数来计算并返回对应值
//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);
}
});
...
服务
服务是一个单一的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);
}
});
提供者
提供者所使用的AngularJS内部创建过程中配置阶段的服务,工厂等(相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;
};
});
});
常量
常量用于通过配置相位值考虑事实,值不能使用期间的配置阶段被传递。
mainApp.constant("configParam", "constant value");
例子
下面的例子将展示上述所有指令。
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 src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/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浏览器打开textAngularJS.html。看到结果如下。

关于详解AngularJS中的依赖注入机制的用户互动如下:
相关问题:如何在程序中使用依赖注入?
答:依赖注入分为三种。 set注入,最常用的,需要在对应的类中声明一个本类的对象,然后添加set方法。通过Spring的配置bean来实例化它。 还有构造器注入,需要添加构造器,默认的是无参的,可以重载构造器,然后在配置文件中配置好constructer标签。... >>详细
相关问题:JAVA中的依赖注入主要作用是什么?
答:你如果理解Spring的IOC机制就明白这个了。差不太多。 可以去看看有关spring框架的资料,网上很多。 >>详细
相关问题:有没有比较轻量级的依赖注入框架或类库
答:3种方法 1。构造器注入 public class xx { private Manager manage; public xx(Manager manage){ this.manage= manage; } } 2.setter 方法注入 public class xx { private Manager manage; public void setManager(Manager manage){ this.manage... >>详细
- 【firefox】firefox浏览器不支持innerText的解决
- 【Extjs】Extjs学习过程中新手容易碰到的低级错误
- 【clearInterval】js clearInterval()方法的定义
- 【dom】javascript dom追加内容实现示例-追加内容
- 【checkbox】让checkbox不选中即将选中的checkbox
- 【Array】js中更短的 Array 类型转换-类型转换
- 【append】append和appendTo的区别以及appendChil
- 【ExtJs】ExtJs 表单提交登陆实现代码-表单提交-
- 【ajax清除浏览器缓存】Ajax清除浏览器js、css、
- 【addClass】javascript自定义的addClass()方法
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
