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 fromnewingfn.c’s stored value comes from first getting an instance bynewingfn, and then running a$getmethod 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()一篇关于三者区别的英文资料 :http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/
cache.b = new fn()
cache.c = (new fn()).$get()
看不来的可以看下中文翻译: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区别的更多相关文章
- angular 服务 service factory provider constant value
angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): ...
- AngularJS之Provider, Value, Constant, Service, Factory, Decorator的区别与详解
本文转载自http://camnpr.com/javascript/1693.html 首先,provider, value, constant, service, factory他们都是provid ...
- AngularJs:Service、Factory、Provider依赖注入使用与区别
本教程使用AngularJS版本:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ ...
- AngularJS中的Provider们:Service和Factory等的区别
引言 看了很多文章可能还是不太说得出AngularJS中的几个创建供应商(provider)的方法(factory(),service(),provider())到底有啥区别,啥时候该用啥,之前一直傻 ...
- 【AngularJS中的自定义服务service VS factory VS provider】---它们的区别,你知道么?
在介绍AngularJS自定义服务之前,我们先来了解一下AngularJS~ 学过HTML的人都知道,HTML是一门很好的伪静态文本展示设计的声明式语言,但是,要构建WEB应用的话它就显得乏力了. 而 ...
- AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)
目录[-] 一.service引导 二.service 1.factory() 2.service() 3.provider() 一.service引导 刚开始学习Angular的时候,经常 ...
- AngularJS中service,factory,provider的区别
一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is whe ...
- anjular中Service、Factory、Provider的使用与js中创建对象的总结
在学习anjular中Service的使用时,发现和js中的创建对象的方式有一定的联系,所以总结了anjular中Service.Factory.Provider的使用方式与js创建对象的方式 一.先 ...
- 转载:[AngularJS系列] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)
来源:http://hellobug.github.io/blog/angularjs-providers/ 用AngularJS做项目,但凡用过什么service啊,factory啊,provide ...
随机推荐
- 【Python】使用torrentParser1.02对单文件torrent的分析结果
C:\Users\horn1\Desktop\python\41-torrentParser>python torrentParser.py 文件名=./5.torrent 文件结构: anno ...
- 使用jsp内置对象request获取表单提交中文内容乱码的解决办法
page1.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists
Add a Column to a table if not exists MySQL allows you to create a table if it does not exist, but d ...
- HttpClient Restful Post 请求
public static void main(String[] args) { SbVo sb = new SbVo(); sb.setBusiness("SB"); sb.se ...
- Word转PDF非常好用的软件——pdfFactory Pro
pfdFactory Pro把word转为pdf的操作步骤: 1.打开将要转换的word的文档: 2.文件--->打印: 弹出如下对话框: 单击确定后弹出:
- Python定向爬虫实战
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7019963.html 一:requests模块介绍 requests是第三方http库,可以十分方便地实现py ...
- Windows虚拟内存不足问题的处理
一般而言,当系统缺少虚拟内存会引起很多问题,包含但不限于: 操作系统运行缓慢,甚至出现Hang(夯机),蓝色背景界面的情况 用户本地控制台输入用户名.密码后,无法登录到操作系统,卡在欢迎界面 无法远程 ...
- java 八种基本数据类型之初始值、取值范围、对应的封装类
CreateTime--2017年12月6日10:03:53 Author:Marydon 一.java数据类型之基本数据类型 (二)八种基本数据类型的特征 import java.math.Bi ...
- Azure编程笔记(3):用Fiddler调试Azure的应用程序
内容提要 Azure的服务是通过RESTfulAPI提供的. 尽管Azure针对非常多编程语言都提供了SDK.但这些SDK也仅仅是RESTfulAPI的一层封装. 在调用SDK或者RESTful ...
- 【Linux】压缩多个文件
在使用Linux的过程中,可能需要将多个文件压缩到一个文件,这样方便复制与移动 多文件压缩案例 现在有文件列表如下: 将文件file_00.txt.file_01.txt.file_02.txt.fi ...