什么是依賴注入
wiki 上的解釋是:依賴注入(Dependency Injection柠偶,簡(jiǎn)稱DI)是一種軟件設(shè)計(jì)模式户秤,在這種模式下,一個(gè)或更多的依賴(或服務(wù))被注入(或者通過(guò)引用傳遞)到一個(gè)獨(dú)立的對(duì)象(或客戶端)中颠悬,然后成為了該客戶端狀態(tài)的一部分矮燎。該模式分離了客戶端依賴本身行為的創(chuàng)建,這使得程序設(shè)計(jì)變得松耦合赔癌,并遵循了依賴反轉(zhuǎn)和單一職責(zé)原則诞外。與服務(wù)定位器模式形成直接對(duì)比的是,它允許客戶端了解客戶端如何使用該系統(tǒng)找到依賴
一句話 --- 沒(méi)事你不要來(lái)找我灾票,有事我會(huì)去找你浅乔。
AngularJS 提供很好的依賴注入機(jī)制。以下5個(gè)核心組件用來(lái)作為依賴注入
value
Value 是一個(gè)簡(jiǎn)單的 javascript 對(duì)象铝条,用于向控制器傳遞值(配置階段):
// 定義一個(gè)模塊
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ù)來(lái)計(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 中通過(guò) 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(常量)用來(lái)在配置階段傳遞數(shù)值馒索,注意這個(gè)常量在配置階段是不可用的。
mainApp.constant("configParam", "constant value");
實(shí)例
AngularJS 實(shí)例 - provider
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.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);
}
});
AngularJS 實(shí)例 - factory
var mainApp = angular.module("mainApp", []);
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);
}
});