欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular 理解module和injector,即依賴注入

 更新時間:2016年09月07日 10:00:59   投稿:lqh  
本文主要介紹Angular 理解module和injector的知識,這里整理了相關知識,并詳細介紹了依賴注入的問題,有興趣的小伙伴可以參考下

依賴注入(DI)的好處不再贅言,使用過spring框架的都知道。angularjs作為前臺js框架,也提供了對DI的支持,這是javascript/jquery不具備的特性。angularjs中與DI相關有angular.module()、angular.injector()、 $injector、$provide。對于一個DI容器來說,必須具備3個要素:服務的注冊、依賴關系的聲明、對象的獲取。比如spring中,服務的注冊是通過xml配置文件的<bean>標簽或是注解@Repository、@Service、@Controller、@Component實現(xiàn)的;對象的獲取可以ApplicationContext.getBean()實現(xiàn);依賴關系的聲明,即可以在xml文件中配置,也可以使用@Resource等注解在java代碼中聲明。在angular中,module和$provide相當于是服務的注冊;injector用來獲取對象(angular會自動完成依賴的注入);依賴關系的聲明在angular中有3種方式。下面從這3個方面,介紹下angular的DI。

1、angular.module()創(chuàng)建、獲取、注冊angular中的模塊

The angular.module() is a global place for creating, registering and retrieving Angular modules.When passed two or more arguments, a new module is created. If passed only one argument, an existing module (the name passed as the first argument to module) is retrieved。

// 傳遞參數(shù)不止一個,代表新建模塊;空數(shù)組代表該模塊不依賴其他模塊
var createModule = angular.module("myModule", []);

// 只有一個參數(shù)(模塊名),代表獲取模塊
// 如果模塊不存在,angular框架會拋異常
var getModule = angular.module("myModule");

// true,都是同一個模塊
alert(createModule == getModule);

該函數(shù)既可以創(chuàng)建新的模塊,也可以獲取已有模塊,是創(chuàng)建還是獲取,通過參數(shù)的個數(shù)來區(qū)分。

angular.module(name, [requires], [configFn]);

name:字符串類型,代表模塊的名稱;

requires:字符串的數(shù)組,代表該模塊依賴的其他模塊列表,如果不依賴其他模塊,用空數(shù)組即可;

configFn:用來對該模塊進行一些配置。

現(xiàn)在我們知道如何創(chuàng)建、獲取模塊了,那么模塊究竟是什么呢?官方的Developer Guide上只有一句話:You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, etc.現(xiàn)在我還不太理解,大致就是說模塊是一些功能的集合,如控制器、服務、過濾器、指令等子元素組成的整體?,F(xiàn)在解釋不了,先遺留。

2、$provide和模塊的關系

The $provide service has a number of methods for registering components with the $injector. Many of these functions are also exposed on angular.Module.

之前提到過:module和provide是用來注冊服務到injector中的。查看官方的API,可以看到$provide提供了provide()、constant()、value()、factory()、service()來創(chuàng)建各種不同性質(zhì)的服務;angular.Module中也提供了這5個服務注冊方法。其實2者功能是完全一樣的,就是用來向DI容器注冊服務到injector中。

官方API下的auto有$provide 和 $injector,Implicit module which gets automatically added to each $injector.按照字面意思是說,每一個injector都有這2個隱含的服務。但1.2.25版本中,感覺沒有辦法獲取injector中的$provide。不知道這是為什么?一般來說也不需要顯示使用這個服務,直接使用module中提供的API即可。

var injector = angular.injector();
alert(injector.has("$provide"));//false
alert(injector.has("$injector"));//true

3、angular.injector()

使用angular.injector();也能獲取到注入器,但是沒有和模塊綁定。這種做法是沒有意義的,相當于是你創(chuàng)建了一個空的DI容器,里面都沒有服務別人怎么用呢。正確的做法是,在創(chuàng)建注入器的時候,指定需要加載的模塊。

// 創(chuàng)建myModule模塊、注冊服務
var myModule = angular.module('myModule', []);
myModule.service('myService', function() {
  this.my = 0;
});

// 創(chuàng)建herModule模塊、注冊服務
var herModule = angular.module('herModule', []);
herModule.service('herService', function() {
  this.her = 1;
});

// 加載了2個模塊中的服務
var injector = angular.injector(["myModule","herModule"]);
alert(injector.get("myService").my);
alert(injector.get("herService").her);

如果加載了多個模塊,那么通過返回的injector可以獲取到多個模塊下的服務。這個例子中如果只加載了myMoudle,那么得到的injector就不能訪問herMoudle下的服務。這里特別需要注意下:angular.injector()可以調(diào)用多次,每次都返回新建的injector對象。

var injector1 = angular.injector(["myModule","herModule"]);
var injector2 = angular.injector(["myModule","herModule"]);

alert(injector1 == injector2);//false

4、angular中三種聲明依賴的方式

angular提供了3種獲取依賴的方式:inference、annotation、inline方式。

// 創(chuàng)建myModule模塊、注冊服務
var myModule = angular.module('myModule', []);
myModule.service('myService', function() {
  this.my = 0;
});

// 獲取injector
var injector = angular.injector(["myModule"]);

// 第一種inference
injector.invoke(function(myService){alert(myService.my);});

// 第二種annotation
function explicit(serviceA) {alert(serviceA.my);};
explicit.$inject = ['myService'];
injector.invoke(explicit);

// 第三種inline
injector.invoke(['myService', function(serviceA){alert(serviceA.my);}]);

其中annotation和inline方式,對于函數(shù)參數(shù)名稱沒有要求,是推薦的做法;inference方式強制要求參數(shù)名稱和服務名稱一致,如果JS代碼經(jīng)過壓縮或者混淆,那么功能會出問題,不建議使用這種方式。

以上就是對Angular JS依賴注入的資料整理,后續(xù)繼續(xù)補充相關資料,謝謝大家對本站的支持!

相關文章

  • 詳解Angular操作cookies方法

    詳解Angular操作cookies方法

    這篇文章主要介紹了詳解Angular操作cookies方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • angularjs實現(xiàn)分頁和搜索功能

    angularjs實現(xiàn)分頁和搜索功能

    這篇文章主要介紹了angularjs實現(xiàn)分頁和搜索功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • AngularJS ng-template寄宿方式用法分析

    AngularJS ng-template寄宿方式用法分析

    這篇文章主要介紹了AngularJS ng-template寄宿方式用法,結合實例形式分析了ng-template模板的相關使用技巧,需要的朋友可以參考下
    2016-11-11
  • AngularJS框架中的雙向數(shù)據(jù)綁定機制詳解【減少需要重復的開發(fā)代碼量】

    AngularJS框架中的雙向數(shù)據(jù)綁定機制詳解【減少需要重復的開發(fā)代碼量】

    這篇文章主要介紹了AngularJS框架中的雙向數(shù)據(jù)綁定機制,結合實例形式分析了AngularJS雙向數(shù)據(jù)綁定機制的原理及實現(xiàn)方法,以及減少需要重復開發(fā)代碼量的優(yōu)勢,需要的朋友可以參考下
    2017-01-01
  • 詳解Ubuntu安裝angular-cli遇到的坑

    詳解Ubuntu安裝angular-cli遇到的坑

    這篇文章主要介紹了詳解Ubuntu安裝angular-cli遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • AngularJS頁面?zhèn)鲄⒌?種方式

    AngularJS頁面?zhèn)鲄⒌?種方式

    Angular頁面?zhèn)鲄⒂卸喾N辦法,根據(jù)不同用例,本文介紹5種最常見的頁面?zhèn)鲄⒌姆绞健>哂泻芎玫膮⒖純r值。下面跟著小編一起來看下吧
    2017-04-04
  • Angular實現(xiàn)的敏感文字自動過濾與提示功能示例

    Angular實現(xiàn)的敏感文字自動過濾與提示功能示例

    這篇文章主要介紹了Angular實現(xiàn)的敏感文字自動過濾與提示功能,結合實例形式分析了AngularJS針對字符串的輸入判定及實時顯示相關操作技巧,需要的朋友可以參考下
    2017-12-12
  • angular 未登錄狀態(tài)攔截路由跳轉(zhuǎn)的方法

    angular 未登錄狀態(tài)攔截路由跳轉(zhuǎn)的方法

    今天小編就為大家分享一篇angular 未登錄狀態(tài)攔截路由跳轉(zhuǎn)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • angular 服務的單例模式(依賴注入模式下)詳解

    angular 服務的單例模式(依賴注入模式下)詳解

    這篇文章主要介紹了angular 服務的單例模式(依賴注入模式下)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • AngularJS初始化靜態(tài)模板詳解

    AngularJS初始化靜態(tài)模板詳解

    這篇文章主要為大家介紹了AngularJS初始化靜態(tài)模板,AngularJS初始化靜態(tài)模板有兩種方式,一是通過ng-app來自動初始化模塊,也可以通過angular.bootstrap(document, [module])手動啟動應用,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評論