Angularjs Service vs Provider vs Factory

Source: Internet
Author: User

Translate later.

Services

Syntax: module.service( ‘serviceName‘, function );
Result:when declaring ServiceName as an injectable argument you'll be a provided with an instance of the function. in the other words new FunctionYouPassedToService() .

Factories

Syntax: module.factory( ‘factoryName‘, function );
Result:when declaring Factoryname as an injectable argument you'll be provided with the value that's returned by I Nvoking The function reference passed to Module.factory.

Providers

syntax:  module.provider (' providerName ', function);  
Result:when declaring ProviderName as an injectable argument  you'll be the provided with &N Bsp providerfunction (). $get () . The constructor function is instantiated before the $get method are Called-providerfunction is the function reference pas Sed to Module.provider.

Providers has the advantage that they can be configured during the module configuration phase.

See here for the provided Code:http://jsbin.com/ohamub/1/edit

Here's a great further explanation by Misko:

Provide.value (' A ', 123); function Controller (a) {expect (a). Toequal (123);}

The injector simply returns the value as is. But what if you want to compute the value? Then use a factory

Provide.factory (' B ', function (a) {return a*2;}); function Controller (b) {expect (b). toequal (246);}

So is factory a function which are responsible for creating the value. Notice The factory function can ask for other dependencies.

But what if want to is more OO and has a class called Greeter?

function Greeter (a) {This.greet = function () {return ' Hello ' + A; }}

Then to instantiate you would has to write

Provide.factory (' greeter ', function (a) {return new greeter (a);});

Then we could ask for ' greeter ' on controller like this

function Controller (greeter) {expect (greeter instanceof greeter). ToBe (True); Expect (Greeter.greet ()). Toequal (' Hello 123 ');}

But this is the too wordy. A shorter-to-write this would isprovider.service(‘greeter‘, Greeter);

but What if we wanted to configure the  Greeter  class before the injection? Then we could write

Provide.provider (' Greeter2 ', function () {var salutation = ' Hello ';  This.setsalutation = function (s) {salutation = s;    } function Greeter (a) {This.greet = function () {return salutation + ' + A;  }} this. $get = function (a) {return new Greeter (a); };});

We can then does this:

Angular.module (' abc ', []). config (function (greeter2provider) {greeter2provider.setsalutation (' Halo ');}); function Controller (greeter2) {expect (Greeter2.greet ()). Toequal (' Halo 123 ');}

As a side note, service , factory , and is all value derived from provider.

Provider.service = function (name, Class) {provider.provide (name, function () {this. $get = function ($injector) {    Return $injector. Instantiate (Class);  }; });}      Provider.factory = function (name, factory) {provider.provide (name, function () {this. $get = function ($injector) {    Return $injector. Invoke (Factory);  }; });}  Provider.value = function (name, value) {provider.factory (name, function () {return value; });};

Reference: Http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory

Angularjs Service vs provider vs Factory

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.