AngularJs Injecting Services Into Controllers詳解
把service當(dāng)作被依賴的資源加載到controller中的方法,與加載到其他服務(wù)中的方法很相似。
由于javascript是一個動態(tài)語言,DI不能弄明白應(yīng)該通過static types(like in static typed languages)注入哪一個service。因此,我們需要通過$inject屬性指定service名稱, 它是一個包含需要注入的service名稱的字符串?dāng)?shù)組。service ID順序的重要性:工廠方法中的參數(shù)順序,與service在數(shù)組中的順序一致。工廠方法的參數(shù)名稱并不重要,但是按照慣常的做法,他們與service ID一一匹配,下面將討論這樣做的好處。
1.顯式依賴注入
function myController($scope,$loc,$log) { $scope.firstMethod = function() { //使用$location service $loc.setHash(); }; $scope.secondMethod = function() { //使用$log service $log.info(‘…') }; } myController.$inject = [‘$location','$log'];
例子:
<!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> <meta charset="UTF-8"> <title>explicit-inject-service</title> </head> <body> <div ng-controller="MyController"> <input type="text" ng-model="msg"/> <button ng-click="saveMsg()">save msg</button> <ul> <li ng-repeat="msg in msgs">{{msg}}</li> </ul> </div> <script src="../angular-1.0.1.js" type="text/javascript"></script> <script type="text/javascript"> var app = angular.module("MainApp",[],function($provide) { $provide.factory("notify",["$window","$timeout",function(win,timeout) { //這里是服務(wù)依賴服務(wù),通過這種顯式的方式,參數(shù)名可以亂填,但順序要對應(yīng) var msgs = []; return function(msg) { msgs.push(msg); if(msgs.length==3) { timeout(function() { win.alert(msgs.join("\n")); msgs = []; },10); } } }]); }); function MyController($s,$noti) { //這里是controller依賴服務(wù),通過這種顯式的方式,參數(shù)名可以亂填,但順序要對應(yīng) $s.msgs = []; $s.saveMsg = function() { this.msgs.push(this.msg); $noti(this.msg); this.msg = ""; }; } //指定注入的東東 //也可以參考http://www.cnblogs.com/lcllao/archive/2012/10/16/2725317.html里面的例子 MyController.$inject = ['$scope','notify']; </script> </body> </html>
2. 隱式依賴注入
angular DI的一個新特性,允許通過參數(shù)名稱決定依賴。讓我們重寫上面的例子,展示如何隱式注入$window、$scope與notify service。
例子:
<!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> <meta charset="UTF-8"> <title>implicit-inject-service</title> </head> <body> <div ng-controller="MyController"> <input type="text" ng-model="msg"/> <button ng-click="saveMsg()">save msg</button> <ul> <li ng-repeat="msg in msgs">{{msg}}</li> </ul> </div> <script src="../angular-1.0.1.js" type="text/javascript"></script> <script type="text/javascript"> var app = angular.module("MainApp",[],function($provide) { $provide.factory("notify",function($window,$timeout) { //服務(wù)依賴服務(wù),隱式依賴,名稱一致即可 var msgs = []; return function(msg) { msgs.push(msg); if(msgs.length==3) { $timeout(function() { $window.alert(msgs.join("\n")); msgs = []; },10); } } }); }); function MyController($scope,notify) { //服務(wù)依賴服務(wù),隱式依賴,名稱一致即可 $scope.msgs = []; $scope.saveMsg = function() { this.msgs.push(this.msg); notify(this.msg); this.msg = ""; }; } </script> </body> </html>
雖然這樣很方便,但是假如我們需要壓縮、混淆我們的代碼,這可能會導(dǎo)致參數(shù)名稱被更改,遇到這種情況的時候,我們還是需要使用顯式聲明依賴的方式。
以上就是關(guān)于AngularJS Injecting Services Into Controllers的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
- AngularJs根據(jù)訪問的頁面動態(tài)加載Controller的解決方案
- AngularJS控制器controller正確的通信的方法
- AngularJS利用Controller完成URL跳轉(zhuǎn)
- angularjs學(xué)習(xí)筆記之三大模塊(modal,controller,view)
- AngularJS入門心得之directive和controller通信過程
- AngularJs學(xué)習(xí)第五篇從Controller控制器談?wù)?scope作用域
- Angularjs中controller的三種寫法分享
- AngularJS ng-controller 指令簡單實例
- AngularJS實踐之使用NgModelController進行數(shù)據(jù)綁定
- AngularJS Controller作用域
相關(guān)文章
利用Ionic2 + angular4實現(xiàn)一個地區(qū)選擇組件
ionic是一個移動端開發(fā)框架,使用hybird技術(shù),只要使用前端開發(fā)技術(shù)就可以開發(fā)出電腦端,安卓端和ios端的站點程序。下面這篇文章主要給大家介紹了關(guān)于利用Ionic2 + angular4實現(xiàn)一個地區(qū)選擇組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07Angular學(xué)習(xí)教程之RouterLink花式跳轉(zhuǎn)
這篇文章主要給大家介紹了關(guān)于Angular學(xué)習(xí)教程之RouterLink花式跳轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05AngularJS基礎(chǔ) ng-init 指令簡單示例
本文主要介紹AngularJS ng-init 指令,這里幫大家整理了關(guān)于ng-init 指令的基本知識資料,并附有簡單的代碼示例,有需要學(xué)習(xí)的小伙伴可以參考下2016-08-08用WebStorm進行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式)
這篇文章主要介紹了用WebStorm進行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Angular4集成ng2-file-upload的上傳組件
本篇文章主要介紹了Angular4集成ng2-file-upload的上傳組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法
在AngularJS中,有時候需要監(jiān)視Scope中的某個變量,因為變量的改變會影響一些界面元素的顯示,接下來通過本文給大家介紹AngularJS中監(jiān)視Scope變量以及外部調(diào)用Scope方法,需要的朋友參考下吧2016-01-01Angular應(yīng)用里環(huán)境變量SERVER_REQUEST_ORIGIN含義解析
這篇文章主要為大家介紹了Angular應(yīng)用里環(huán)境變量SERVER_REQUEST_ORIGIN的含義解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Angular中使用ui router實現(xiàn)系統(tǒng)權(quán)限控制及開發(fā)遇到問題
這篇文章主要介紹了Angular中使用ui router實現(xiàn)系統(tǒng)權(quán)限控制及開發(fā)遇到問題的相關(guān)資料,本文分步驟介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2016-09-09