Difference between a Factory, Service and Provider in AngularJS

When I first started playing around with AngularJS, the title of this post used to bug me all the time. What exactly is the difference between a Service, Factory, and Provider? When do I use one over the other? Of all the five ways to create a service, we usually are confused with using Service, Factory and Provider.

Well, this is what I have learnt:


Syntax: module.service( ‘serviceName’, function );

Difference : You can add properties and functions to a service object by using ‘this’ keyword. Unlike factory, it doesn’t return anything.

When to use : It is a singleton object. Use it when you need to share a single object across the application. For example, utility functions.


Syntax : module.factory( ‘factoryName’, function );

Difference : A factory is a simple function which allows you to add some logic before creating the object. It returns the created object.

When to use : It is a collection of functions like a class. Hence, it can be instantiated in different controllers when you are using it with constructor function.


Syntax : module.provider( ‘providerName’, function );

Difference : A provider is used to create a configurable service object. It returns value by using $get() function.

When to use : When you need to provide module-wise configuration for your service object before making it available.

Following is a complete example on how to use the above three types of services.

Play with the code on Plunker : http://plnkr.co/edit/LqPLeD?p=preview

For a complete list of what goes where, refer to this excellent articleD by Dmitri here

Leave a Reply