Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Difference between Service, Factory and Provider in AngularJS
// Source:!topic/angular/hVrkvaHGOfc
// jsFiddle:
// 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. = 'Default';
this.$get = function() {
var name =;
return {
sayHello: function() {
return "Hello, " + name + "!"
this.setName = function(name) { = name;
//hey, we can configure a provider!
function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {
$scope.hellos = [
Copy link

dmitriz commented Apr 22, 2014

Here is a very simple example of provider without 'this':

Copy link

sunew commented May 26, 2014

@gigablox : Thanks for the super example, that really got it working for me. A question/comment: Instead of using events to signal data is ready, wouldn't it be a good just to return a promise. And then let the controller set then on the promise, and set data on the scope from then?

Copy link

hanmina commented Jun 17, 2014

thanks for useful code block. but I would be happy if you explain each type, purpose and when to use which one.

Copy link

joshids commented Jun 24, 2014

  1. When you register the Service you return an object, when Angular needs to inject "service" into any component, it will inject this object.
  2. When you register the Factory, you return a constructor function. When angular needs to inject "service" into any component, it will first do a

new factoryReturnedFunction();

and then inject it. This will be done only for the first time service is requested. For all subsequent injections, object created first time will be used.
3. When you register a Provider, it has same behavior as Factory above. The function assigned to ...$get..., will be used as constructor function.

Copy link

expalmer commented Jul 9, 2014

thanks for sharing, you help me alot to understand.

Copy link

ghost commented Jul 21, 2014

Nice Info

Copy link

Webenius commented Jul 28, 2014

@Mithrandir0x, thank you for the example!
If we need a service and we define it using the "factory" method, is it write or false?

Copy link

codeandcats commented Jun 11, 2015

Thanks, this is very helpful. :)

Copy link

Hemalatah commented Aug 25, 2015

Thanks much for the explanation. would you tell me, where to use which, I mean, how to make a right choice among them?
Appreciate your time

Copy link

xis19 commented Nov 18, 2015

Thanks for the helpful example.

Copy link

vahidalvandi commented Mar 3, 2016

very thanks

Copy link

umangparekh001 commented Feb 1, 2017

Many Thanks !

Copy link

ghost commented Mar 29, 2017

Nice explanation about Service / Provider / Factory, Very useful, Thank you very much !

Copy link

shashankGopannagari commented Nov 16, 2017

Nice Explanation. Helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment