AngularJS: 什么是工厂?

我在 Angular.js上做了很多工作,总的来说,我发现它是一个有趣而强大的框架。

我知道有很多关于服务、工厂、提供商和价值观的讨论,但是我仍然很困惑什么是 Factory

在其他 StackOverflow 讨论中,Factory 被定义为:

工厂

语法: module.factory( 'factoryName', function );结果: 当将 factoryName 声明为一个可注入参数时,通过调用传递给 module.Factory 的函数引用,将向您提供返回的值。

我发现这个解释很难理解,它并没有增加我对工厂是什么的理解。

是否有任何人有任何解释或现实生活中的例子来分享到底什么是一个 Factory和为什么你应该使用它来代替一个 ServiceProvider,或其他?

更新

对任何 对象

返回任意 对象factory 是一个函数

返回任意 功能provider 是一个函数

- -

69900 次浏览

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.

Check out this question AngularJS: Service vs provider vs factory

Also this gist may be helpful in understanding the subtle differences.

Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc

jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/

author: Pawel Kozlowski

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()];
}​

One major difference I see is that you can run custom code in the factory. But, in a service, only object creation happens.

myJs.factory('Factory', function() {


//Write custom code here


return {
Hello: function() {
return "Hello, World!"
}
};
});

My two cents on this topic. I am a very very newbie and just into understanding Angular JS and this was one of the things that confused me a lot and hence I studied it in somewhat detail. I have been making notes for giving interviews and this may be useful to others.

  • service and factory do the same things in different ways
  • both are injectibles
  • for most things use factory syntax
  • easier to understand
  • nowadays with es6 "service" is done since it converts to es6 classes better
  • its essentially abstracting away business logic from the controller
  • if you use biz logic with controllers then you can only use with controllers
  • controller is for putting data on scope not processing lengthy biz logic
  • so what happens in above scenario is that complex biz logic is tied up into controllers. Not for processing data. So put bits and pieces of it into services or factory. So your code is lean and modular.
  • services are singletons

Services are mostly objects in which you describe the constructor class of the object. Somewhere deep within the framework, the Object.create() function is called and then you can use a service by calling its object and methods using a controller. Factory, on the other hand, doesn't create an object by default and hence requires you to return the entire object location once you're done defining all the attributes and methods.