溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Angular中自定義服務(wù)Service、Provider以及Factory有什么用

發(fā)布時間:2021-08-04 14:15:42 來源:億速云 閱讀:140 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“Angular中自定義服務(wù)Service、Provider以及Factory有什么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Angular中自定義服務(wù)Service、Provider以及Factory有什么用”這篇文章吧。

先說說Factory:

通過注冊.factory('my注冊名',方法()),返回一個對象,你就能在控制器中引入這個方法并訪問這個對象:
例子:

<!-- factory模式 -->
  <div ng-controller="theFactoryCtrl">
    <h4>Factory模式</h4>
    <ul>
      <li ng-repeat="i in names">
        {{i}}
      </li>
    </ul>

  </div>

JS代碼:

/*工廠模式,注入?yún)?shù)中調(diào)用的是這個函數(shù)里的返回值*/
  app.factory("myFactory",function(){

    var args = arguments;
    var obj = {}

    obj.exec = function(){
      var arr = [];
      for(let i = 0; i<arguments.length; i++){
        arr.push(arguments[i]);
      }
      return arr;
    }
    return obj;
  })
app.controller("theFactoryCtrl",function ($scope,myFactory) {

    $scope.names = myFactory.exec("張三的歌","趙四的舞","老王賊六");

  })

效果:

Angular中自定義服務(wù)Service、Provider以及Factory有什么用

Service:

在service使用構(gòu)造函數(shù)的方法去用它,在控制器中是以new的方式引入,所以可以調(diào)用service中定義的屬性

HTML:

<!-- service模式 -->
  <div ng-controller="theServiceCtrl">
    <h4>Service模式</h4>
    <p>prop:{{prop}}</p>
    <p>num:{{num}}</p>
  </div>

JS:

app.controller("theServiceCtrl",function($scope,myService){
    $scope.prop = myService.prop("呵呵");
    $scope.num = myService.num;
  })
/*Service是new出來的,所以可以直接調(diào)用里面的屬性*/
  app.service("myService",function(){
    this.num = Math.floor(Math.random()*10);//0到10的隨機(jī)數(shù)
    this.prop = function(arg){
      return arg;
    };
  })

效果:

Angular中自定義服務(wù)Service、Provider以及Factory有什么用

Provider:

如果你想在創(chuàng)建服務(wù)之前先對服務(wù)進(jìn)行配置,那么你需要provider,因?yàn)閜rovider可以通過定義config,并在$get中訪問config
HTML:

<!-- provider模式 -->
  <div ng-controller="theProviderCtrl">
    <h4>Provider模式</h4>
    <p>姓名:{{info.name}}</p>
    <p>部隊:{{info.squad}}</p>
    <p>職務(wù):{{info.role}}</p>
  </div>

JS:

/*使用$get方法關(guān)聯(lián)對應(yīng)的config*/
  app.provider("myProvider",function(){

    this.$get = function(){

      return {
        name : "朱子明",
        squad : "八路軍386旅獨(dú)立團(tuán)",
        role : this.roleSet

      }
    }

  })
  /*名字必須符合規(guī)范:xxxxxxProvider*/
  app.config(function(myProviderProvider){
    myProviderProvider.roleSet = "保衛(wèi)干事"
  })
app.controller("theProviderCtrl",function($scope,myProvider){
    $scope.info = {
      name : myProvider.name,
      squad : myProvider.squad,
      role : myProvider.role

    }
  })

效果:

Angular中自定義服務(wù)Service、Provider以及Factory有什么用

以上是“Angular中自定義服務(wù)Service、Provider以及Factory有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI