Angular service, factory pre-provider Difference

Source: Internet
Author: User

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:

    1. a' s stored value comes from running fn .
    2. b' s stored value comes from new ing fn .
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.