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 numbersmainApp.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 的朋友。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注