AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)
目录[-]
一、service引导
刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别。This is where we'll start the twenty-five days of Angular calendar.
二、service
在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除。而controllers在不需要的时候就会被销毁了。
这就是为什么使用controllers在应用里面传递数据不可靠的原因,特别是使用routing的时候。Services are designed to be the glue between controllers, the minions of data, the slaves of functionality, the worker-bees of our application(就是说services在应用的controllers、 方法、数据之前起到了很关键的作用)
现在我们开始看怎么创建service。每个方法我们都会看到下面两个一样的参数:
name-我们要定义的service的名字
function-service方法
他们都创建了相同的底层对象类型。实例化后,他们都创建了一个service,这些对象没有什么功能上的差别。
1、factory()
Angular里面创建service最简单的方式是使用factory()方法。
factory()让我们通过返回一个包含service方法和数据的对象来定义一个service。在service方法里面我们可以注入services,比如 $http 和 $q等。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
angular.module('myApp.services').factory('User', function($http) { // injectables go here var backendUrl = "http://localhost:3000"; var service = { // our factory definition user: {}, setName: function(newName) { service.user['name'] = newName; }, setEmail: function(newEmail) { service.user['email'] = newEmail; }, save: function() { return $http.post(backendUrl + '/users', { user: service.user }); } }; return service;}); |
在应用里面使用factory()方法
在应用里面可以很容易地使用factory ,需要到的时候简单地注入就可以了
|
1
2
3
4
|
angular.module('myApp').controller('MainCtrl', function($scope, User) { $scope.saveUser = User.save;}); |
什么时候使用factory()方法
在service里面当我们仅仅需要的是一个方法和数据的集合且不需要处理复杂的逻辑的时候,factory()是一个非常不错的选择。
注意:需要使用.config()来配置service的时候不能使用factory()方法
2、service()
service()通过构造函数的方式让我们创建service,我们可以使用原型模式替代javaScript原始的对象来定义service。
和factory()方法一样我们也可以在函数的定义里面看到服务的注入
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
angular.module('myApp.services').service('User', function($http) { // injectables go here var self = this; // Save reference this.user = {}; this.backendUrl = "http://localhost:3000"; this.setName = function(newName) { self.user['name'] = newName; } this.setEmail = function(newEmail) { self.user['email'] = newEmail; } this.save = function() { return $http.post(self.backendUrl + '/users', { user: self.user }) }}); |
这里的功能和使用factory()方法的方式一样,service()方法会持有构造函数创建的对象。
在应用里面使用service()方法
|
1
2
3
4
|
angular.module('myApp').controller('MainCtrl', function($scope, User) { $scope.saveUser = User.save;}); |
什么时候适合使用service()方法
service()方法很适合使用在功能控制比较多的service里面
注意:需要使用.config()来配置service的时候不能使用service()方法
3、provider()
provider()是创建service最底层的方式,这也是唯一一个可以使用.config()方法配置创建service的方法
不像上面提到的方法那样,我们在定义的this.$get()方法里面进行依赖注入
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
angular.module('myApp.services').provider('User', function() { this.backendUrl = "http://localhost:3000"; this.setBackendUrl = function(newUrl) { if (url) this.backendUrl = newUrl; } this.$get = function($http) { // injectables go here var self = this; var service = { user: {}, setName: function(newName) { service.user['name'] = newName; }, setEmail: function(newEmail) { service.user['email'] = newEmail; }, save: function() { return $http.post(self.backendUrl + '/users', { user: service.user }) } }; return service; }}); |
在应用里面使用provider()方法
为了给service进行配置,我们可以将provider注入到.config()方法里面
|
1
2
3
4
|
angular.module('myApp').config(function(UserProvider) { UserProvider.setBackendUrl("http://myApiBackend.com/api");}) |
这样我们就可以和其他方式一样在应用里面使用这个service了
|
1
2
3
4
|
angular.module('myApp').controller('MainCtrl', function($scope, User) { $scope.saveUser = User.save;}); |
什么时候使用provider()方法
当我们希望在应用开始前对service进行配置的时候就需要使用到provider()。比如,我们需要配置services在不同的部署环境里面(开发,演示,生产)使用不同的后端处理的时候就可以使用到了
当我们打算发布开源provider()也是首选创建service的方法,这样就可以使用配置的方式来配置services而不是将配置数据硬编码写到代码里面。
还可以看看这篇翻译:http://www.oschina.net/translate/top-10-mistakes-angularjs-developers-make
点击查看完整的代码:https://gist.github.com/auser/7743235
AngularJS中service,factory,provider的区别(转载:http://my.oschina.net/tanweijie/blog/295067)的更多相关文章
- AngularJS进阶(三十三)书海拾贝之简介AngularJS中使用factory和service的方法
简介AngularJS中使用factory和service的方法 AngularJS支持使用服务的体系结构"关注点分离"的概念.服务是JavaScript函数,并负责只做一个特定的 ...
- angularjs---服务(service / factory / provider)
初angularJs时 常写一些不够优雅的代码 !我总结了一下看看各位有没有中枪的!-----( 这里只针对服务service及其相关! ) 以下做法不太优雅 兄弟controller 之间的相同 ...
- AngularJS 中的 factory、 service 和 provider区别,简单易懂
转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...
- AngularJS中service,factory,provider的区别
一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is whe ...
- AngularJS之Provider, Value, Constant, Service, Factory, Decorator的区别与详解
本文转载自http://camnpr.com/javascript/1693.html 首先,provider, value, constant, service, factory他们都是provid ...
- AngularJS中的指令全面解析(转载)
说到AngularJS,我们首先想到的大概也就是双向数据绑定和指令系统了,这两者也是AngularJS中最为吸引人的地方.双向数据绑定呢,感觉没什么好说的,那么今天我们就来简单的讨论下AngularJ ...
- 简介AngularJS中使用factory和service的方法
AngularJS支持使用服务的体系结构“关注点分离”的概念.服务是JavaScript函数,并负责只做一个特定的任务.这也使得他们即维护和测试的单独实体.控制器,过滤器可以调用它们作为需求的基础.服 ...
- angularjs model.service vs provider vs factory?
<!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...
- angular 服务 service factory provider constant value
angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): ...
随机推荐
- 【转】MYSQL入门学习之一:基本操作
转载地址:http://www.2cto.com/database/201212/173868.html 1.登录数据库 www.2cto.com 命令:mysql -u usern ...
- string xml json格式区别
string 是一种最普通的储存一串字符的数据格式 xml 是一种可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 它非常适合万维网传输,提供统一的方 ...
- Uva 1599 最佳路径
题目链接:https://uva.onlinejudge.org/external/15/1599.pdf 题意: 保证在最短路的时候,输出字典序最小的路径. 方法: 路径上有了权值,可以利用图论的数 ...
- CSUFT2016训练赛
解题报告: Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 39958 Accepted: 13 ...
- http模式
http遵守请求响应模式: 请求: 请求行:HTTP方法-请求的URL-HTTP版本 请求行 空行 消息体 响应: 状态行:HTTP版本-响应码-响应描述 响应头 空行 消息体 其他的重点: http ...
- DailyReport自动保存工具
PS:自己初学C#,SharePoint时做的一个小tool. Friday, November 28, 2014 这个tool编译出来以后可以把部门的daily report保存到本地,数据库,和 ...
- 学习java annotation
Annotation介绍 内置注解 自定义注解 元注解 /** * 测试自定义注解的使用 * */ @SxtAnnotation01 public class Demo02 { @Annotation ...
- HTML 5 Web Sockets应用初探
转载不错的http://developer.51cto.com/art/201008/217874.htm
- 2016年10月20日 星期四 --出埃及记 Exodus 19:4
2016年10月20日 星期四 --出埃及记 Exodus 19:4 `You yourselves have seen what I did to Egypt, and how I carried ...
- 使用cookie保存页面登录信息
1.数据库连接配置页面:connectvars.php <?php//数据库的位置define('DB_HOST', 'localhost');//用户名define('DB_USER', 'r ...