原文是ng-newsletter.com上的25 Days Of Angular的第一篇,感兴趣的可以去原文查看。
原文地址:http://www.ng-newsletter.com/advent2013/#!/day/1
此文仅为加深自己理解而翻译,需要学习的请去查看原文。
水平有限,翻译或理解有错误的地方欢迎大家帮忙指正,有好的翻译文章也欢迎大家给我推荐。
The short guide to service definitions
service definitions
初学者最容易误解的关于Angular components是service(),factory()和provide(),接下来我们将开始“the twenty-five days of Angular calendar”的第一天。
The Service
在Angular中,services是在需要的时候被创建,知道应用程序的生命周期结束(关闭浏览器)才会被销毁的单例对象。Controllers会在他们不被需要的时候被销毁。
这是为什么我们不能用controllers在应用程序中共享数据,尤其是在使用路由的时候。Services are designed to be the glue between controllers, the minions of data, the slaves of functionality, the worker-bees of our application.
我们深入研究Service,方法有两个参数,相同的方法签名:
- name - 我们定义的service的名字
- function - service定义的函数
每个创建相同的潜在对象类型,他们被实例化后,他们都创建了一个service,对象类型之间没有function difference。
factory()
factory()方法是最简单的创建Service的途径。
factory()允许我们通过返回一个包含service function和service data的对象来定义一个service,我们可以在service definition function注入services,例如$http,$q。
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()
It's easy to use the factory in our application as we can simply inject it where we need it at run-time.
angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
$scope.saveUser = User.save;
});
何时使用factory()
当我们仅仅是需要数据和方法的集合,不需要任何复杂的操作的时候,用factory()是最好的选择。
*当需要用.config()配置service时,不要使用factory()。*
service()
service()允许我们通过定义constrcutor function来创建service,我们能用原型对象来定义service,而不是使用原始的javascript对象。
和factory()一样,我们在function definiton中设置注射器:
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
})
}
});
Functionally equivalent to using the factory() method, the service() method will hold on to the object created by the constructor object.
在应用中使用service():
angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
$scope.saveUser = User.save;
});
何时使用service()
The service() method is great for creating services where we need a bit more control over the functionality required by our service. It's also mostly guided by preference to use this instead of referring to the service.
*当需要用.config()配置service时,不要使用factory()。*
provide()
最低等的创建service的方法是provide(),它也是唯一的能用.config()函数配置的创建service的方法。
和前面的方法不同,我们需要在this.$get()设置注射器。
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,我们在.config()中注入provider。
angular.module('myApp')
.config(function(UserProvider) {
UserProvider.setBackendUrl("http://myApiBackend.com/api");
})
现在我们能在应用中像其他service那样使用我们的service了:
angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
$scope.saveUser = User.save;
});
何时使用provider()
The
provider()method is required when we want to configure our service before the app runs. For instance, if we need to configure our services to use a different back-end based upon different deployment environments (development, staging, and production).It's the preferred method for writing services that we intend on distributing open-source as well as it allows us to configure services without needing to hard-code configuration data.
浙公网安备 33010602011771号