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

關(guān)于AngularJS中幾種Providers的區(qū)別總結(jié)

 更新時(shí)間:2020年05月17日 09:34:13   作者:lavender  
這篇文章主要給大家介紹了關(guān)于AngularJS中幾種Providers的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用AngularJS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

原文:https://xebia.com/blog/differ...

什么是Provider?

AngularJS文檔對(duì)provider的定義:

provider是一個(gè)帶有$get()方法的對(duì)象。injector調(diào)用$get方法創(chuàng)建一個(gè)新的service的實(shí)例。provider還有一些其他的方法,可以用來(lái)配置provider。

AngularJS使用$provide注冊(cè)新的providers。providers基本上都會(huì)創(chuàng)建一個(gè)新實(shí)例, 但每個(gè)provider只創(chuàng)建一次。$provide提供了6種方法創(chuàng)建自定義provider, 我會(huì)用簡(jiǎn)單的代碼示例分別解釋他們。

6種方法如下:

  • constant
  • value
  • service
  • factory
  • decorator
  • provider

Constant

constant能被injected到任何地方。constant不能被decorator攔截, 意味著constant的值永遠(yuǎn)不能被改變。

var app = angular.module('app', []); 
app.config(function ($provide) {
 $provide.constant('movieTitle', 'The Matrix');
});

app.controller('ctrl', function (movieTitle) {
 expect(movieTitle).toEqual('The Matrix');
});

AngularJS提供了一種更簡(jiǎn)便的方式創(chuàng)建constant. 你可以將上面3至5行的代碼重寫(xiě)為:

app.constant('movieTitle', 'The Matrix');

Value

value是一個(gè)簡(jiǎn)單的可被注入的值,可以是string, number, 也可以是function。

與constant不同的是:value不能被注入到configurations, 但value能被decorators攔截。

var app = angular.module('app', []); 
app.config(function ($provide) {
 $provide.value('movieTitle', 'The Matrix')
});

app.controller('ctrl', function (movieTitle) {
 expect(movieTitle).toEqual('The Matrix');
})

創(chuàng)建value的簡(jiǎn)單方法:

app.value('movieTitle', 'The Matrix');

Service

service是一個(gè)可以注入的構(gòu)造函數(shù)。如果你想,你可以在函數(shù)中指定需要的依賴(lài)。

service是一個(gè)單例, 只被創(chuàng)建一次。services是一個(gè)很好的方式,用于控制器之間傳遞數(shù)據(jù),如共享數(shù)據(jù)。

var app = angular.module('app' ,\[\]); 
app.config(function ($provide) {
 $provide.service('movie', function () {
  this.title = 'The Matrix';
 });
});

app.controller('ctrl', function (movie) {
 expect(movie.title).toEqual('The Matrix');
});

創(chuàng)建service簡(jiǎn)單方式:

app.service('movie', function () {
 this.title = 'The Matrix';
});

Factory

factory是一個(gè)可注入的函數(shù)。

與service的相同點(diǎn):factory也是一個(gè)單例,也可以在此函數(shù)中指定依賴(lài)。

區(qū)別是:factory注入一個(gè)普通函數(shù),AngularJs將調(diào)用此函數(shù),而service注入一個(gè)構(gòu)造函數(shù)。

service是一個(gè)構(gòu)造函數(shù),要調(diào)用new創(chuàng)建一個(gè)新對(duì)象。而用factory,你可以讓這個(gè)函數(shù)返回你想要的任何東西。
你將會(huì)看到,factory是一個(gè)只有$get方法的provider。

var app = angular.module('app', []); 
app.config(function ($provide) {
 $provide.factory('movie', function () {
  return {
   title: 'The Matrix';
  }
 });
});

app.controller('ctrl', function (movie) {
 expect(movie.title).toEqual('The Matrix');
});

創(chuàng)建factory的簡(jiǎn)單方式:

app.factory('movie', function () {
 return {
  title: 'The Matrix';
 }
});

Decorator

decorator可以修改或封裝其它的providers,但constant不能被裝飾。

var app = angular.module('app', []); 
app.value('movieTitle', 'The Matrix'); 
app.config(function ($provide) {
 $provide.decorator('movieTitle', function ($delegate) {
  return $delegate + ' - starring Keanu Reeves';
 });
});

app.controller('myController', function (movieTitle) {
 expect(movieTitle).toEqual('The Matrix - starring Keanu Reeves');
});

Provider
provider是所有providers中最復(fù)雜的,可以有復(fù)雜的creation函數(shù)和配置選項(xiàng)。

provider實(shí)際是一個(gè)可配置的factory。 provider接受一個(gè)對(duì)象或構(gòu)造函數(shù)。

var app = angular.module('app', []); 
app.provider('movie', function () {
 var version;
 return {
  setVersion: function (value) {
   version = value;
  },
  $get: function () {
   return {
    title: 'The Matrix' + ' ' + version
   }
  }
 }
});

app.config(function (movieProvider) {
 movieProvider.setVersion('Reloaded');
});

app.controller('ctrl', function (movie) {
 expect(movie.title).toEqual('The Matrix Reloaded');
});

總結(jié)

所有的providers只會(huì)被實(shí)例化一次,因此他們都是單例的。

除了constant,其他的providers都可以被decorated。

constant是一個(gè)值, 可以被注入到任何地方,它的值不能被改變。

value是一個(gè)簡(jiǎn)單的可注入的值。

service是一個(gè)可注入的構(gòu)造函數(shù)。

factory是以個(gè)可注入的函數(shù)。

decorator可以修改或封裝其它的providers,除了constant。

provider是一個(gè)可配置的factory。

到此這篇關(guān)于關(guān)于AngularJS中幾種Providers的區(qū)別總結(jié)的文章就介紹到這了,更多相關(guān)AngularJS中Providers區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Commands Queries設(shè)計(jì)模式提高Angular應(yīng)用性能及可維護(hù)性

    Commands Queries設(shè)計(jì)模式提高Angular應(yīng)用性能及可維護(hù)性

    在Angular應(yīng)用開(kāi)發(fā)領(lǐng)域,Commands and Queries 設(shè)計(jì)模式是一個(gè)關(guān)鍵的概念,它有助于有效地管理應(yīng)用程序的狀態(tài)和與后端的交互,本文將深入探討這一設(shè)計(jì)模式的核心要點(diǎn),并通過(guò)實(shí)際示例來(lái)加以說(shuō)明
    2023-10-10
  • Angular.js 4.x中表單Template-Driven Forms詳解

    Angular.js 4.x中表單Template-Driven Forms詳解

    Angular 4.x 中有兩種表單,一種是Template-Driven Forms - 模板驅(qū)動(dòng)式表單,另外一種是Reactive Forms - 響應(yīng)式表單 ,下面這篇文章主要給大家介紹了Angular.js 4.x中表單Template-Driven Forms的相關(guān)資料,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。
    2017-04-04
  • AngularJS實(shí)現(xiàn)動(dòng)態(tài)切換樣式的方法分析

    AngularJS實(shí)現(xiàn)動(dòng)態(tài)切換樣式的方法分析

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)動(dòng)態(tài)切換樣式的方法,結(jié)合實(shí)例形式分析了AngularJS事件響應(yīng)與樣式動(dòng)態(tài)控制相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • angularjs過(guò)濾器--filter與ng-repeat配合有奇效

    angularjs過(guò)濾器--filter與ng-repeat配合有奇效

    本篇文章主要介紹了angularjs過(guò)濾器-filter與ng-repeat的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • 基于Angular中ng-controller父子級(jí)嵌套的相關(guān)屬性詳解

    基于Angular中ng-controller父子級(jí)嵌套的相關(guān)屬性詳解

    今天小編就為大家分享一篇基于Angular中ng-controller父子級(jí)嵌套的相關(guān)屬性詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • AngularJs表單校驗(yàn)功能實(shí)例代碼

    AngularJs表單校驗(yàn)功能實(shí)例代碼

    這篇文章主要介紹了AngularJs表單校驗(yàn)功能實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • 詳談Angular 2+ 的表單(一)之模板驅(qū)動(dòng)型表單

    詳談Angular 2+ 的表單(一)之模板驅(qū)動(dòng)型表單

    這篇文章主要介紹了Angular 2+ 的表單(一)之模板驅(qū)動(dòng)型表單,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-04-04
  • ionic4+angular7+cordova上傳圖片功能的實(shí)例代碼

    ionic4+angular7+cordova上傳圖片功能的實(shí)例代碼

    ionic是一個(gè)垮平臺(tái)開(kāi)發(fā)框架,可通過(guò)web技術(shù)開(kāi)發(fā)出多平臺(tái)的應(yīng)用。這篇文章主要介紹了ionic4+angular7+cordova上傳圖片功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-06-06
  • 淺談angular4實(shí)際項(xiàng)目搭建總結(jié)

    淺談angular4實(shí)際項(xiàng)目搭建總結(jié)

    本篇文章主要介紹了淺談angular4實(shí)際項(xiàng)目搭建總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 基于AngularJs select綁定數(shù)字類(lèi)型的問(wèn)題

    基于AngularJs select綁定數(shù)字類(lèi)型的問(wèn)題

    今天小編就為大家分享一篇基于AngularJs select綁定數(shù)字類(lèi)型的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10

最新評(píng)論