日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Angularjs中provider,factory和service的不同

 huhuwoo 2015-10-10

provider, factory和service都是寫(xiě)Angularjs的service中常用的關(guān)鍵字,很容易混淆,寫(xiě)了一個(gè)簡(jiǎn)單的例子顯示他們之間的區(qū)別:

分別用service,factory和provider定義三個(gè)service:

復(fù)制代碼
var wtcModule = angular.module('wtc', []);

wtcModule.service('testService',function(){
     this.lable = 'this is service';
});

wtcModule.factory('testFactory', function () {
     return{
        lable: function(){
        return 'this is factory';
        }
    }
});

wtcModule.provider('testProvider', function(){
    this.$get = function(){
        return 'this is provider';
    }
});
復(fù)制代碼

在頁(yè)面上留出三個(gè)占位符:

<body ng-controller='outputCtrl'>
    <p>{{ output1 }}</p>
    <p>{{ output2 }}</p>
    <p>{{ output3 }}</p>
</body>

寫(xiě)好outputCtrl:

復(fù)制代碼
var wtcModule = angular.module('wtc');

wtcModule.controller('outputCtrl', function($scope,testService, testFactory, testProvider){
    $scope.output1 = testService.lable;
    $scope.output2 = testFactory.lable();
    $scope.output3 = testProvider;
});
復(fù)制代碼

最后頁(yè)面的顯示結(jié)果為;

說(shuō)明:

注入service,相當(dāng)于注入service定義時(shí)的function實(shí)例。

注入factory,相當(dāng)于注入factory定義時(shí)的函數(shù)調(diào)用入口。

注入provider,相當(dāng)于注入provider內(nèi)$get定義的函數(shù)實(shí)例的調(diào)用。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多