The project team did a angular share last night, just to discuss the issue. Although not to do front-end development, but interest caused. Access to the following information for subsequent use
Own understanding: The service is new, factory is directly used to get to the service object, the service has a more this. Provider can do some global configuration before initializing the injection, and there is a need to get the $get method
A relatively simple one to understand
app.factory(‘a‘, fn);app.service(‘b‘, fn);app.provider(‘c‘, fn);
The difference between the three is:
a
' s stored value comes from running fn
.
b
' s stored value comes from new
ing fn
.
c
' s stored value comes from first getting an instance new
by ing fn
, and then running a $get
method of the instance.
Which means there ' s something like a cache object inside AngularJS, whose value of each injection are only assigned once, W Hen they ' ve been injected the first time, and where:
cache. A = Fn () cache.b = new Fn ( ) cache.= (new FN $get ()
An English material about the three differences: http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/
You can see the Chinese translation:/http Www.oschina.net/translate/angularjs-factory-vs-service-vs-provider
But not recommended, or honestly look at English as well
last to a longer
var myApp = angular.module (' MyApp ', []);//service style, probably the simplest onemyapp.service (' Helloworldfromservice ' , function () {This.sayhello = function () {return "Hello, world!" };});/ /factory style, more involved but more sophisticatedmyapp.factory (' Helloworldfromfactory ', function () {return { Sayhello:function () {return "Hello, world!" } };});/ /provider style, full blown, configurable versionmyapp.provider (' HelloWorld ', function () {//In the Provider function, You cannot inject any//service or factory. This can is only is 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 ()];}
Summary information from colleagues:
The difference between service and factory
Somemodule.factory (' testf ', [function ()} {
var f = 1;
You can return any type of JS support, such as [],{},function. (Recommended as an object)
return {
Add:function () {
f++;
Console.log (f);
}
};
Not in this form.
var f = 1;
This.add = function () {
f++;
Console.log (f);
// };
}]). Service (' TestS ', [function () {
This can
var s = 1;
This.add = function () {
S+
+;
Console.log (s);
};
This can also be used. As with factory, you can return any type of JS support, such as [],{},function. (Recommended as an object)
var s = 1;
return {
Add:function () {
s++;
Console.log (s);
// }
// };
}])
Summary "Service is in the form of a new function, and the service provides a constructor. Factory is created by executing the provided function.
That is to say: Service is more than factory a kind of this. How members are written, service-created instances multi-level prototypes (prototypes of constructors)
PS: The role of one-stage prototype in Ng is not known, unknown.
Show the objects that are obtained in these two ways:
Finally, StackOverflow's divine comment on the discussion, http://stackoverflow.com/questions/15666048/ Service-vs-provider-vs-factory Considering that many friends can fq difficult, the beloved blogger helps you to transfer a PDF.
Angular service, factory pre-provider Difference