2016年1月编辑:因为这仍然受到关注。自从问了这个问题后,我已经完成了一些AngularJS项目,对于那些我主要使用factory的项目,建立一个对象并在结束时返回对象。然而,我下面的陈述仍然是正确的。
编辑:我想我终于理解了两者之间的主要区别,我有一个代码示例来演示。我也认为这个问题与提议的副本不同。这个副本说服务是不可实例化的,但是如果您按照下面演示的方式设置它,它实际上是可实例化的。可以将服务设置为与工厂完全相同。我还将提供显示工厂故障转移服务的代码,这似乎是其他答案所不能做到的。
如果我像这样设置VaderService(即作为一个服务):
var module = angular.module('MyApp.services', []);
module.service('VaderService', function() {
this.speak = function (name) {
return 'Join the dark side ' + name;
}
});
然后在我的控制器中,我可以这样做:
module.controller('StarWarsController', function($scope, VaderService) {
$scope.luke = VaderService.speak('luke');
});
使用service,注入到控制器中的VaderService被实例化,所以我可以只调用VaderService。然而,如果我将VaderService更改为模块。工厂,控制器中的代码将不再工作,这是主要的区别。使用factory,注入到控制器中的VaderService不会被实例化,这就是为什么在设置工厂时需要返回一个对象(请参阅问题中的示例)。
然而,你可以像设置工厂一样设置服务(IE让它返回一个对象),并且服务的行为与工厂完全相同
根据这些信息,我认为没有理由使用工厂而不是服务,服务可以做工厂可以做的所有事情,甚至更多。
下面是原始问题。
我知道这个问题已经被问过很多次了,但我真的看不出工厂和服务之间有任何功能上的区别。我读过这个教程:http://blogs.clevertech.biz/startupblog/angularjs-factory-service-provider
它似乎给出了一个合理的解释,然而,我的应用程序是这样设置的:
index . html
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<script src="lib/angular/angular.js"></script>
<script type="text/javascript" src="js/controllers.js"></script>
<script type="text/javascript" src="js/VaderService.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body ng-app="MyApp">
<table ng-controller="StarWarsController">
<tbody>
<tr><td>{{luke}}</td></tr>
</tbody>
</table>
</body>
</html>
app.js:
angular.module('MyApp', [
'MyApp.services',
'MyApp.controllers'
]);
controllers.js:
var module = angular.module('MyApp.controllers', []);
module.controller('StarWarsController', function($scope, VaderService) {
var luke = new VaderService('luke');
$scope.luke = luke.speak();
});
VaderService.js
var module = angular.module('MyApp.services', []);
module.factory('VaderService', function() {
var VaderClass = function(padawan) {
this.name = padawan;
this.speak = function () {
return 'Join the dark side ' + this.name;
}
}
return VaderClass;
});
然后当我加载index.html时,我看到“加入黑暗面卢克”,很好。果然不出所料。然而,如果我把VaderService.js改为这个(注意模块。Service而不是module.factory):
var module = angular.module('MyApp.services', []);
module.service('VaderService', function() {
var VaderClass = function(padawan) {
this.name = padawan;
this.speak = function () {
return 'Join the dark side ' + this.name;
}
}
return VaderClass;
});
然后重新加载index.html(我确保清空了缓存并进行了硬加载)。它的工作原理与module.factory完全相同。那么两者之间真正的功能差异是什么?
服务vs工厂
工厂和服务之间的区别就像函数和对象之间的区别一样
工厂的供应商
Gives us the function's return value ie. You just create an object, add properties to it, then return that same object.When you pass this service into your controller, those properties on the object will now be available in that controller through your factory. (Hypothetical Scenario)
Singleton and will only be created once
Reusable components
Factory are a great way for communicating between controllers like sharing data.
Can use other dependencies
Usually used when the service instance requires complex creation logic
Cannot be injected in .config() function.
Used for non configurable services
If you're using an object, you could use the factory provider.
Syntax: module.factory('factoryName', function);
服务提供者
Gives us the instance of a function (object)- You just instantiated with the ‘new’ keyword and you’ll add properties to ‘this’ and the service will return ‘this’.When you pass the service into your controller, those properties on ‘this’ will now be available on that controller through your service. (Hypothetical Scenario)
Singleton and will only be created once
Reusable components
Services are used for communication between controllers to share data
You can add properties and functions to a service object by using the this keyword
Dependencies are injected as constructor arguments
Used for simple creation logic
Cannot be injected in .config() function.
If you're using a class you could use the service provider
Syntax: module.service(‘serviceName’, function);
示例演示
在下面的例子中,我定义了MyService和MyFactory。注意在.service中我是如何使用this.methodname创建服务方法的。在.factory中,我创建了一个工厂对象,并将方法分配给它。
AngularJS .service
module.service('MyService', function() {
this.method1 = function() {
//..method1 logic
}
this.method2 = function() {
//..method2 logic
}
});
AngularJS .factory
module.factory('MyFactory', function() {
var factory = {};
factory.method1 = function() {
//..method1 logic
}
factory.method2 = function() {
//..method2 logic
}
return factory;
});
再看看这个漂亮的东西
搞不清服务和工厂
AngularJS的工厂、服务和提供者
js: service vs provider vs factory?
工厂和服务只是提供者的包装。
工厂
Factory可以返回任何可以是类(构造函数)、类实例、字符串、数字或布尔值的东西。如果返回构造函数,则可以在控制器中实例化。
myApp.factory('myFactory', function () {
// any logic here..
// Return any thing. Here it is object
return {
name: 'Joe'
}
}
服务
服务不需要返回任何东西。但是你必须赋值这个变量中的所有值。因为service会默认创建instance并将其用作基对象。
myApp.service('myService', function () {
// any logic here..
this.name = 'Joe';
}
服务背后的实际angularjs代码
function service(name, constructor) {
return factory(name, ['$injector', function($injector) {
return $injector.instantiate(constructor);
}]);
}
它只是工厂周围的一个包装。如果你从服务中返回一些东西,那么它的行为就像工厂。
重要提示:工厂和服务的返回结果将被缓存,并且将为所有控制器返回相同的结果。
什么时候使用?
工厂在任何情况下都是可取的。当你有需要在不同控制器中实例化的构造函数时,可以使用它。
服务是一种单例对象。从Service返回的对象对于所有控制器都是一样的。当你想为整个应用程序拥有一个对象时,可以使用它。
已验证的用户详细信息。
欲进一步了解,请阅读
http://iffycan.blogspot.in/2013/05/angular-service-or-factory.html
http://viralpatel.net/blogs/angularjs-service-factory-tutorial/
服务vs工厂
工厂和服务之间的区别就像函数和对象之间的区别一样
工厂的供应商
Gives us the function's return value ie. You just create an object, add properties to it, then return that same object.When you pass this service into your controller, those properties on the object will now be available in that controller through your factory. (Hypothetical Scenario)
Singleton and will only be created once
Reusable components
Factory are a great way for communicating between controllers like sharing data.
Can use other dependencies
Usually used when the service instance requires complex creation logic
Cannot be injected in .config() function.
Used for non configurable services
If you're using an object, you could use the factory provider.
Syntax: module.factory('factoryName', function);
服务提供者
Gives us the instance of a function (object)- You just instantiated with the ‘new’ keyword and you’ll add properties to ‘this’ and the service will return ‘this’.When you pass the service into your controller, those properties on ‘this’ will now be available on that controller through your service. (Hypothetical Scenario)
Singleton and will only be created once
Reusable components
Services are used for communication between controllers to share data
You can add properties and functions to a service object by using the this keyword
Dependencies are injected as constructor arguments
Used for simple creation logic
Cannot be injected in .config() function.
If you're using a class you could use the service provider
Syntax: module.service(‘serviceName’, function);
示例演示
在下面的例子中,我定义了MyService和MyFactory。注意在.service中我是如何使用this.methodname创建服务方法的。在.factory中,我创建了一个工厂对象,并将方法分配给它。
AngularJS .service
module.service('MyService', function() {
this.method1 = function() {
//..method1 logic
}
this.method2 = function() {
//..method2 logic
}
});
AngularJS .factory
module.factory('MyFactory', function() {
var factory = {};
factory.method1 = function() {
//..method1 logic
}
factory.method2 = function() {
//..method2 logic
}
return factory;
});
再看看这个漂亮的东西
搞不清服务和工厂
AngularJS的工厂、服务和提供者
js: service vs provider vs factory?