angularJS中service、provice、factor区别

provider, factory和service都是写AngularJS的service中常用的关键字,很容易混淆,写了一个简单的例子显示他们之间的区别:

分别用service,factory和provider定义三个service:

  1. var wtcModule = angular.module('wtc', []);  
  2.   
  3. wtcModule.service('testService',function(){  
  4.      this.lable = 'this is service';  
  5. });  
  6.   
  7. wtcModule.factory('testFactory', function () {  
  8.      return{  
  9.         lable: function(){  
  10.         return 'this is factory';  
  11.         }  
  12.     }  
  13. });  
  14.   
  15. wtcModule.provider('testProvider', function(){  
  16.     this.$get = function(){  
  17.         return 'this is provider';  
  18.     }  
  19. });  
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';
    }
});
在页面上留出三个占位符:

  1. <body ng-controller='outputCtrl'>  
  2.     <p>{{ output1 }}</p>  
  3.     <p>{{ output2 }}</p>  
  4.     <p>{{ output3 }}</p>  
  5. </body>  
<body ng-controller='outputCtrl'>
    <p>{{ output1 }}</p>
    <p>{{ output2 }}</p>
    <p>{{ output3 }}</p>
</body>

写好outputCtrl:

  1. var wtcModule = angular.module('wtc');  
  2.   
  3. wtcModule.controller('outputCtrl', function($scope,testService, testFactory, testProvider){  
  4.     $scope.output1 = testService.lable;  
  5.     $scope.output2 = testFactory.lable();  
  6.     $scope.output3 = testProvider;  
  7. });  
var wtcModule = angular.module('wtc');

wtcModule.controller('outputCtrl', function($scope,testService, testFactory, testProvider){
    $scope.output1 = testService.lable;
    $scope.output2 = testFactory.lable();
    $scope.output3 = testProvider;
});

最后页面的显示结果为;

说明:

注入service,相当于注入service定义时的function实例。

注入factory,相当于注入factory定义时的函数调用入口。

注入provider,相当于注入provider内$get定义的函数实例的调用。

转自:http://blog.csdn.net/u012841667/article/details/52749996

//初始数据 var dsy = new Dsy(); dsy.add("0",["北京市","天津市","上海市","重庆市","河北省","山西省","内蒙古","辽宁省","吉林省","黑龙江省","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","广西","海南省","四川省","贵州省","云南省","西藏","陕西省","甘肃省","青海省","宁夏","新疆","香港","澳门","台湾省"]); dsy.add("0_0",["北京市"]); dsy.add("0_0_0",["东城区","西城区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","怀柔区","平谷区","密云区","延庆区"]); dsy.add("0_1",["天津市"]); dsy.add("0_1_0",["和平区","河东区","河西区","南开区","河北区","红桥区","东丽区","西青区","津南区","北辰区","武清区","宝坻区","滨海新区","宁河区","静海区","蓟县"]); dsy.add("0_2",["上海市"]); dsy.add("0_2_0",["黄浦区","徐汇区","长宁区","静安区","普陀区","虹口区","杨浦区","闵行区","宝山区","嘉定区","浦东新区","金山区","松江区","青浦区","奉贤区","崇明区"]); dsy.add("0_3",["重庆市"]); dsy.add("0_3_0",["万州区","湆陵区","渝区","大渡口区","江北区","沙坪坝区","九龙坡区","南岸区","北碚区","綦江区","大足区","渝北区","巴南区","黔江区","长寿区","江津区","合川区","永川区","南川区","璧山区","铜梁区","潼南区","荣昌区","开州区","梁平区","武隆区","城口县","丰都县","垫江县","忠县","云阳县","奉节县","巫山县","巫溪县","石柱土家族自治县","彭水苗族土家族自治县","酉阳土家族苗族自治县","秀山土家族苗族自治县"]); dsy.add("0_4",["石家庄市","张家口市","承德市","秦皇岛市","唐山市","廊坊市","保定市","衡水市","沧州市","邢台市","邯郸市"]); dsy.add("0_4_0",["桥东区","桥西区","新华区","裕华区","井陉矿区","辛集市","藁城市","鹿泉区","栾城区","晋州市","新乐市","井陉县","正定县","行唐县","灵寿县","高邑县","深泽县","赞皇县","无极县","平山县","元氏县","赵县"]); dsy.add("0_4_1",["桥西区","桥东区","宣化区","下花园区","宣化县","张家口市宣化区","张北县","张北镇","康保县","康保镇","沽源县","平定堡镇","尚义县","南壕堑镇","蔚县","蔚州镇","阳原县","西城镇","怀安县","柴沟堡镇","万全县","孔家庄镇","怀来县","沙城镇","涿鹿县","涿鹿镇","赤城县","赤城镇","崇礼县","西湾子镇"]); dsy.add("0_4_2",["双桥区","双滦区","鹰手营子矿区","承德县","下板城镇","兴隆县","兴隆镇","平泉县","平泉镇","滦平县","滦平镇","隆化县","隆化镇","丰宁满族自治县","大阁镇","宽城满族自治县","宽城镇","围场满族蒙古族自治县","围场镇"]); dsy.add("0_4_3",["海港区","山海关区","北戴河区","昌黎县","昌黎镇","抚宁县","抚宁镇","卢龙县","卢龙镇","青龙满族自治县","青龙镇"]); dsy.add("0_4_4",["路北区","路南区","古冶区","开平区","丰润区","丰南区","遵化市","迁安市","滦县","滦州镇","滦南县","倴城镇","乐亭县","乐亭镇","迁西县","兴城镇","玉田县","玉田镇","唐海县","唐海镇"]); dsy.add("0_4_5",["安次区","广阳区","霸州市","三河市","固安县","固安镇","永清县","永清镇","香河县","淑阳镇","大城县","平舒镇","文安县","文安镇","大厂回族自治县","大厂镇"]); dsy.add("0_4_6",["新市区","北市区","南市区","定州市","涿州市","安国市","高碑店市","满城县","满城镇","清苑县","清苑镇","易县","易州镇","徐水县","安肃镇","涞源县","涞源镇","定兴县","定兴镇","顺平县","蒲阳镇","唐县","仁厚镇","望都县","望都镇","涞水县","涞水镇","高阳县","高阳镇","安新县","安新镇","雄县","雄州镇","容城县","容城镇","曲阳县","恒州镇","阜平县","阜平镇","博野县","博陵镇","蠡县","蠡吾镇"]); dsy.add("0_4_7",["桃城区","冀州市","深州市","枣强县","枣强镇","武邑县","武邑镇","武强县","武强镇","饶阳县","饶阳镇","安平县","安平镇","故城县","郑口镇","景县","景州镇","阜城县","阜城镇"]); dsy.add("0_4_8",["运河区","新华区","泊头市","任丘市","黄骅市","河间市","沧县","沧州市新华区","青县","清州镇","东光县","东光镇","海兴县","苏基镇","盐山县","盐山镇","肃宁县","肃宁镇","南皮县","南皮镇","吴桥县","桑园镇","献县","乐寿镇","孟村回族自治县","孟村镇"]); dsy.add("0_4_9",["桥东区","桥西区","南宫市","沙河市","邢台县","邢台市桥东区","临城县","临城镇","内丘县","内丘镇","柏乡县","柏乡镇","隆尧县","隆尧镇","任县","任城镇","南和县","和阳镇","宁晋县","凤凰镇","巨鹿县","巨鹿镇","新河县","新河镇","广宗县","广宗镇","平乡县","丰州镇","威县","洺州镇","清河县","葛仙庄镇","临西县","临西镇"]); dsy.add("0_4_10",["丛台区","邯山区","复兴区","峰峰矿区","武安市","邯郸县","南堡乡东小屯村","临漳县","临漳镇","成安县","成安镇","大名县","大名镇","涉县","涉城镇","磁县","磁州镇","肥乡县","肥乡镇","永年县","临洺关镇","邱县","新马头镇","鸡泽县","鸡泽镇","广平县","广平镇","馆陶县","馆陶镇","魏县","魏城镇","曲周县","曲周镇"]);
AngularJS is the leading framework for building dynamic JavaScript applications that take advantage of the capabilities of modern browsers and devices. AngularJS, which is maintained by Google, brings the power of the Model-View-Controller (MVC) pattern to the client, providing the foundation for complex and rich web apps. It allows you to build applications that are smaller, faster, and with a lighter resource footprint than ever before. Best-selling author Adam Freeman explains how to get the most from AngularJS. He begins by describing the MVC pattern and the many benefits that can be gained from separating your logic and presentation code. He then shows how you can use AngularJS's features within in your projects to produce professional-quality results. Starting from the nuts-and-bolts and building up to the most advanced and sophisticated features AngularJS is carefully unwrapped, going in-depth to give you the knowledge you need. Each topic is covered clearly and concisely and is packed with the details you need to learn to be truly effective. The most important features are given a no-nonsense in-depth treatment and chapters include common problems and details of how to avoid them. What you'll learn •Gain a solid architectural understanding of the MVC Pattern to separate logic, data and presentation code. •Learn how to create rich and dynamic web apps using AngularJS •Understand how each feature works and why it is important •Understand how to extend HTML with declarative syntax •Learn how to extend and customize AngularJS •Learn how to test, refine, and deploy your AngularJS projects Who this book is for This book is ideal for web developers who have a working knowledge of JavaScript, HTML and CSS. The book is platform agnostic and a range of browsers will be presented and discussed throughout the examples.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值