AngularJS:什么是工厂? [英] AngularJS : What is a factory?
问题描述
我已经做了很多工作,在 Angular.js
和总体来说,我觉得它是一个有趣而强大的框架。
I've been doing a lot of work on Angular.js
and overall I find it to be an interesting and powerful framework.
我知道已经有很多与工厂与供应商与价值观上的服务的讨论,但我还是pretty无所适从一个工厂
是。
I know there have been a lot of discussions on Services vs. Factories vs. Providers vs. Values, but I am still pretty confused about what a Factory
is.
厂已在其他StackOverflow的讨论如下定义:
Factory has been defined in other StackOverflow discussions as the following:
的工厂的
语法: module.factory('factoryName',功能);
结果:当声明factoryName的注射参数,你会被提供由调用返回的值该函数引用传递给module.factory。
Syntax: module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.
我觉得这个解释是很难把握的,它不会增加我的厂是什么的认识。
I find this explanation to be very difficult to grasp and it doesn't increase my understanding of what a factory is.
有没有人有任何解释或现实生活中的例子来分享一下究竟一个工厂
,以及为什么你应该代替服务的使用
,提供
,还是其他?
Would anyone have any explanations or real life examples to share about what exactly a Factory
is and why you should use it in lieu of a Service
, Provider
, or other?
A 服务
的持有引用的任何对象
A 工厂
的是一个函数的返回任何的目标
A factory
is a function which returns any object
A 提供商
的是一个函数的返回任何的功能
A provider
is a function which returns any function
- 唷的 -
推荐答案
据我了解,他们都是pretty大同小异。的主要差别是它们的复杂性。商都在运行时配置的,工厂是多了几分稳健和服务是最简单的形式。
From what I understand they are all pretty much the same. The major differences are their complexities. Providers are configurable at runtime, factories are a little more robust, and services are the simplest form.
看看这个问题服务提供商VS VS工厂
另外这款要点可以理解的细微差别是有帮助的。
Also this gist may be helpful in understanding the subtle differences.
来源:<一个href=\"https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc\">https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
的jsfiddle:<一href=\"http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/\">http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
作者:帕维尔·科兹洛夫斯基
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()];
}
这篇关于AngularJS:什么是工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!