angular之service、factory预provider区别
昨晚项目组做了angular分享,刚好有讨论到这个问题。虽然许久不做前端开发,但是兴趣所致。就查阅了下资料,以便后续需要使用
自己的理解:service是new出来的,factory是直接使用就能获得到service对象,service多了一个this。provider可以初始化注入之前进行一些全局配置,还有就是需要通过$get方法来获得
比较简单的一个理解
app.factory('a', fn); app.service('b', fn); app.provider('c', fn);
The difference between the three is that:
a
's stored value comes from runningfn
.b
’s stored value comes fromnew
ingfn
.c
’s stored value comes from first getting an instance bynew
ingfn
, and then running a$get
method of the instance.
Which means there’s something like a cache object inside AngularJS, whose value of each injection is only assigned once, when they've been injected the first time, and where:
cache.a = fn() cache.b = new fn() cache.c = (new fn()).$get()
一篇关于三者区别的英文资料 :http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/看不来的可以看下中文翻译:http://www.oschina.net/translate/angularjs-factory-vs-service-vs-provider但是不推荐,还是老老实实看英文为好最后来篇比较长的
var myApp = angular.module('myApp', []);//Service style, probably the simplest one
myApp.service('helloWorldFromService', function() {this.sayHello = function() {return "Hello, World!"};
});//Factory style, more involved but more sophisticated
myApp.factory('helloWorldFromFactory', function() {return {sayHello: function() {return "Hello, World!"}};
});//Provider style, full blown, configurable version
myApp.provider('helloWorld', function() {// In the provider function, you cannot inject any// service or factory. This can only be done at the// "$get" method.this.name = 'Default';this.$get = function() {var name = this.name;return {sayHello: function() {return "Hello, " + name + "!"}}};this.setName = function(name) {this.name = name;};
});//Hey, we can configure a provider!
myApp.config(function(helloWorldProvider){helloWorldProvider.setName('World');
});function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {$scope.hellos = [helloWorld.sayHello(),helloWorldFromFactory.sayHello(),helloWorldFromService.sayHello()];
}
同事的总结资料:
//service和factory的区别
someModule.factory('testF', [function(){var f = 1;//可以return任意js支持的类型,如[],{},function.(建议是一个对象)return {add:function(){f++;console.log(f);}};//不能用这种形式// var f = 1;// this.add = function(){// f++;// console.log(f);// };}]).service('testS', [function(){//这种可以var s = 1;this.add = function(){s++;console.log(s);};//这种也可以用。和factory一样,可以return任意js支持的类型,如[],{},function。(建议是一个对象)// var s = 1;// return {// add:function(){// s++;// console.log(s);// }// };}])// 总结】service是用new function形式的,service提供的方法是构造函数。factory是通过执行提供的函数来创建。// 也就是说:service比factory多了一种this.成员的写法,service创建的实例多一级原型(构造函数的原型)//PS:在ng中多了一级原型的作用 还没了解,未知
下图展示的是这两种方式得到的对象:
angular之service、factory预provider区别相关推荐
- Angularjs1.x 中的 service,factory,provider,constant,value
了解 Angularjs1.x 中的 service,factory,provider,constant,value 的应用场景及区别 不管 service , factory 还是 provider ...
- angular js一factory,service,provider创建服务
服务:在AngularJS 中,服务是一个函数或对象 在写控制器的时候,不要复用controller,当我们的controller里面有相同的代码时,此时需要把它抽取成一个服务,所有的服务都符合依赖注 ...
- AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)...
目录[-] 一.service引导 二.service 1.factory() 2.service() 3.provider() 一.service引导 刚开始学习Angular的时候,经常 ...
- AngularJS Provider/Service/Factory 使用
一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM. 2.浏览器载入angular.js脚本. 3.AngularJS等到DOMContentLoaded事件触发. ...
- angular中service、provice、factor区别
provider, factory和service都是写Angularjs的service中常用的关键字,很容易混淆,写了一个简单的例子显示他们之间的区别: 分别用service,factory和pr ...
- angularjs---服务(service / factory / provider)
初angularJs时 常写一些不够优雅的代码 !我总结了一下看看各位有没有中枪的!-----( 这里只针对服务service及其相关! ) 以下做法不太优雅 兄弟controller 之间的相同 ...
- 关于Failed to check the status of the service com.xxx.service.ItemService. No provider available.....
项目中用dubbo发生: Failed to check the status of the service com.test.service.ItemService. No provider ava ...
- Service 与 Thread 的区别
Service 与 Thread 的区别 很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来 ...
- Spring中的@ Component,@ Repository和@Service批注有什么区别?
@Repository @Component , @Repository和@Service批注可以在Spring中互换使用吗,或者除了充当注解设备外,它们还提供任何特定功能吗? 换句话说,如果我有一个 ...
- Spring注解的使用步骤,@Component注解创建的对象名称,常用注解Component、Repository、Service以及Controller的区别
1. 注解的使用 1.1 pom.xml文件加入spring-context依赖,间接加入了spring-aop依赖,有aop依赖才能使用注解 1.2 在spring配置文件中加入组件扫描器标签< ...
最新文章
- 谈谈战双的战斗机制设计趋同
- 域名恶意指向的问题解决
- html获取qq头像代码,jQuery在线获取QQ名称和头像
- Python基础小结
- (82)FPGA仿真停止激励(stop)
- python 分布式存储_Amazon S3 分布式存储的 python 接口实现
- 0基础学python做什么工作好-零基础学Python,越早明白这些,越快找到好工作!...
- 华为s5700交换机使用配置
- python做var模型的滞后阶数怎么确定_VAR模型滞后阶数
- ir2104s的自举电容_有关IR2104的自举电容和NMOS选择问题要点.docx
- 在javascript中重新加载/刷新页面的不同方法
- J2me xmlReader的轻量级实现
- 短期趋势自用经传捕捞季节 通达信指标公式副图 源码 贴图
- js 原生代码跳转页面,返回上一级页面
- 腾达无线网卡驱动linux,在Ubuntu上安装腾达W541U V2.0 (RT2070)无线网卡驱动【使用2011新驱动】...
- 区块链去中心化和传统去中心化的区别
- sidirect 连接西门子_如何配置DASSIDirect与西门子
- 2022年起重机司机(限门式起重机)考试题模拟考试题库及答案
- Android 接入华为通知栏推送教程(特此整理)
- 美国容错服务器维修,Stratus美国容错首推四核容错服务器