AngularJS 依賴注入詳解和簡單實(shí)例
AngularJS 依賴注入
什么是依賴注入
wiki 上的解釋是:依賴注入(Dependency Injection,簡稱DI)是一種軟件設(shè)計(jì)模式,在這種模式下,一個(gè)或更多的依賴(或服務(wù))被注入(或者通過引用傳遞)到一個(gè)獨(dú)立的對(duì)象(或客戶端)中,然后成為了該客戶端狀態(tài)的一部分。
該模式分離了客戶端依賴本身行為的創(chuàng)建,這使得程序設(shè)計(jì)變得松耦合,并遵循了依賴反轉(zhuǎn)和單一職責(zé)原則。與服務(wù)定位器模式形成直接對(duì)比的是,它允許客戶端了解客戶端如何使用該系統(tǒng)找到依賴
一句話 --- 沒事你不要來找我,有事我會(huì)去找你。
AngularJS 提供很好的依賴注入機(jī)制。以下5個(gè)核心組件用來作為依賴注入:
value
factory
service
provider
constant
value
Value 是一個(gè)簡單的 javascript 對(duì)象,用于向控制器傳遞值(配置階段):
var mainApp = angular.module("mainApp", []);
// 創(chuàng)建 value 對(duì)象 "defaultInput" 并傳遞數(shù)據(jù)
mainApp.value("defaultInput", 5);
...
// 將 "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 是一個(gè)函數(shù)用于返回值。在 service 和 controller 需要時(shí)創(chuàng)建。
通常我們使用 factory 函數(shù)來計(jì)算或返回值。
// 定義一個(gè)模塊
var mainApp = angular.module("mainApp", []);
// 創(chuàng)建 factory "MathService" 用于兩數(shù)的乘積 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;
});
// 在 service 中注入 factory "MathService"
mainApp.service('CalcService', function(MathService){
this.square = function(a) {
return MathService.multiply(a,a);
}
});
...
provider
AngularJS 中通過 provider 創(chuàng)建一個(gè) service、factory等(配置階段)。
Provider 中提供了一個(gè) factory 方法 get(),它用于返回 value/service/factory。
// 定義一個(gè)模塊
var mainApp = angular.module("mainApp", []);
...
// 使用 provider 創(chuàng)建 service 定義一個(gè)方法用于計(jì)算兩數(shù)乘積
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(常量)用來在配置階段傳遞數(shù)值,注意這個(gè)常量在配置階段是不可用的。
mainApp.constant("configParam", "constant value");
實(shí)例
以下實(shí)例提供了以上幾個(gè)依賴注入機(jī)制的演示。
<html>
<head>
<meta charset="utf-8">
<title>AngularJS 依賴注入</title>
</head>
<body>
<h2>AngularJS 簡單應(yīng)用</h2>
<div ng-app = "mainApp" ng-controller = "CalcController">
<p>輸入一個(gè)數(shù)字: <input type = "number" ng-model = "number" /></p>
<button ng-click = "square()">X<sup>2</sup></button>
<p>結(jié)果: {{result}}</p>
</div>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/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>
運(yùn)行結(jié)果:

以上就是對(duì)AngularJS 依賴注入資料整理,后續(xù)繼續(xù)補(bǔ)充,希望能幫助開發(fā)AngularJS 的朋友。
相關(guān)文章
詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
這篇文章主要介紹了JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定,包括作用域的繼承以及數(shù)據(jù)的單向和雙向綁定等重要知識(shí)點(diǎn),需要的朋友可以參考下2016-03-03
AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
angularjs 獲取默認(rèn)選中的單選按鈕的value方法
angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果
Angularjs過濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例
Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例

