为什么要角使用的服务? [英] Why use services in Angular?
问题描述
我刚开始接触角。读谷歌文档服务的例子,我只是想知道,你为什么会选择使用服务,而保持变量和功能权限的控制?
I am just starting out with Angular. Reading the example of a service in the Google documentation, I just wonder why you would choose to use a service rather keeping the variables and function right in the controller?
angular.
module('MyServiceModuleDI', []).
factory('notify', function($window) {
var msgs = [];
return function(msg) {
msgs.push(msg);
if (msgs.length == 3) {
$window.alert(msgs.join("\n"));
msgs = [];
}
};
});
function myController($scope, notify) {
$scope.callNotify = function(msg) {
notify(msg);
};
}
当你会选择在这种情况下使用服务?
When would you choose to use a service in this case?
推荐答案
在我的意见主要理由是:
In my opinions the main reasons are:
-
坚持和控制器之间的数据共享。
结果IE:您创建需要去抓取数据,形成数据库,如果你将其存储在控制器内,一旦更改到另一个控制器的数据将被丢弃(除非你把它存储在$ rootScope服务,但这不是最好的办法做到这一点),但如果你让一个服务(服务是单身)内,当您更改控制系统的数据将持续存在。
Persist and share data between Controllers.
I.E: You create a service that fetchs data form a database, if you store it inside a controller, once you change to another Controller the data will be discarded (unless you store it in the $rootScope but this is not the best way to do it) , but if you keep it inside a service (services are singletons), the data will persist when you change controllers.
通过创建将由您的控制器/指令/服务所使用的API的摘要数据访问逻辑。结果
保持业务逻辑内部控制器和数据逻辑中的服务。
Abstract data access logic by creating an API that will be used by your controllers/directives/services.
Keep business logic inside Controllers and data logic inside services.
DRY(不要重复自己)结果IE:你有一系列您在不同的控制器需要的功能,没有服务,你必须重复你的$在每个控制器C $ C,具有服务,您可以创建此功能的单一的API,并在每个控制器/指令/服务你需要注入它。
DRY (Don't repeat yourself).
I.E: You have a series of functions that you need in different controllers, without the service you would have to repeat your code in each controller, with a service you can create a single API for this functions and inject it in every Controller/Directive/Service you need.
下面是一个例子:
var myApp = angular.module('myApp',[]);
//Here is the service Users with its functions and attributes
//You can inject it in any controller, service is a singleton and its data persist between controllers
myApp.factory('Users', function () {
//data logic
//fetch data from db and populate...
var name = "John";
var surname = "Doe"
function getName() { return name; }
function getFullName() { return name + ' ' + surname; }
function setName(newName) { name = newName; }
//API
return {
getName: getName,
getFullName: getFullName,
setName: setName
}
});
//An Util service with methods I will use in different controllers
myApp.factory('Util', function () {
//a bunch of useful functions I will need everywhere
function daysInMonth (month,year) {
return new Date(year, month+1,0).getDate();
}
return {
daysInMonth: daysInMonth
};
});
//Here I am injecting the User and Util services in the controllers
myApp.controller('MyCtrl1', ['$scope', 'Users', 'Util', function ($scope, Users, Util) {
$scope.user = Users.getFullName(); //"John Doe";
Users.setName('Bittle');
//Using Util service
$scope.days = Util.daysInMonth(05,2013);
}]);
myApp.controller('MyCtrl2', ['$scope', 'Users', 'Util', function ($scope, Users, Util) {
$scope.user = Users.getFullName(); //"Bittle Doe"; //The change that took place in MyCtrl1 hhas persisted.
}]);
这篇关于为什么要角使用的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!