Skip to content

Instantly share code, notes, and snippets.

@hsuh
Created May 30, 2014 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsuh/f8a8128ad81ec50fa5d9 to your computer and use it in GitHub Desktop.
Save hsuh/f8a8128ad81ec50fa5d9 to your computer and use it in GitHub Desktop.
Angularjs Service vs Factory vs Provider
Services
Syntax: module.service( 'serviceName', function ); Result: When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService().
Factories
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.
Providers
Syntax: module.provider( 'providerName', function ); Result: When declaring providerName as an injectable argument you will be provided with ProviderFunction().$get(). The constructor function is instantiated before the $get method is called - ProviderFunction is the function reference passed to module.provider.
Providers have the advantage that they can be configured during the module configuration phase.
See here for the provided code: http://jsbin.com/ohamub/1/edit
From stackoverflow thread - http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment