AngularJS 依賴(lài)注入詳解和簡(jiǎn)單實(shí)例
AngularJS 依賴(lài)注入
什么是依賴(lài)注入
wiki 上的解釋是:依賴(lài)注入(Dependency Injection,簡(jiǎn)稱(chēng)DI)是一種軟件設(shè)計(jì)模式,在這種模式下,一個(gè)或更多的依賴(lài)(或服務(wù))被注入(或者通過(guò)引用傳遞)到一個(gè)獨(dú)立的對(duì)象(或客戶(hù)端)中,然后成為了該客戶(hù)端狀態(tài)的一部分。
該模式分離了客戶(hù)端依賴(lài)本身行為的創(chuàng)建,這使得程序設(shè)計(jì)變得松耦合,并遵循了依賴(lài)反轉(zhuǎn)和單一職責(zé)原則。與服務(wù)定位器模式形成直接對(duì)比的是,它允許客戶(hù)端了解客戶(hù)端如何使用該系統(tǒng)找到依賴(lài)
一句話(huà) --- 沒(méi)事你不要來(lái)找我,有事我會(huì)去找你。
AngularJS 提供很好的依賴(lài)注入機(jī)制。以下5個(gè)核心組件用來(lái)作為依賴(lài)注入:
value
factory
service
provider
constant
value
Value 是一個(gè)簡(jiǎn)單的 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ù)來(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í)例
以下實(shí)例提供了以上幾個(gè)依賴(lài)注入機(jī)制的演示。
<html> <head> <meta charset="utf-8"> <title>AngularJS 依賴(lài)注入</title> </head> <body> <h2>AngularJS 簡(jiǎn)單應(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 依賴(lài)注入資料整理,后續(xù)繼續(xù)補(bǔ)充,希望能幫助開(kāi)發(fā)AngularJS 的朋友。
- JS的反射問(wèn)題
- java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼
- JavaScript對(duì)象反射用法實(shí)例
- AJAX JavaScript反射機(jī)制的介紹
- AngularJs動(dòng)態(tài)加載模塊和依賴(lài)注入詳解
- 詳解AngularJS中的依賴(lài)注入機(jī)制
- 深入理解Javascript里的依賴(lài)注入
- JavaScript中的依賴(lài)注入詳解
- Javascript技術(shù)棧中的四種依賴(lài)注入詳解
- JavaScript中實(shí)現(xiàn)依賴(lài)注入的思路分享
- JavaScript反射與依賴(lài)注入實(shí)例詳解
相關(guān)文章
angular.js實(shí)現(xiàn)購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了angular.js購(gòu)物車(chē)功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
這篇文章主要介紹了JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定,包括作用域的繼承以及數(shù)據(jù)的單向和雙向綁定等重要知識(shí)點(diǎn),需要的朋友可以參考下2016-03-03AngularJS實(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過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例

Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例