AngularJS: Service vs provider vs factory
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In AngularJS, service, factory, and provider are all ways to create and share reusable components. They are part of the dependency injection system and help manage logic, state, or reusable functionality in your app. However, they differ in how they are created and instantiated.
1. Service
A service is a constructor function instantiated using the new keyword. It is simpler and used when you need an object with methods and properties.
Key Characteristics:
- AngularJS uses the
newkeyword to instantiate the service. - You attach properties and methods to
this. - Returns a singleton instance of the service.
Syntax:
Usage:
How It Works:
- AngularJS internally does:
2. Factory
A factory is a function that returns an object. It allows you to create and configure the object before returning it.
Key Characteristics:
- It returns an object explicitly.
- Allows you to construct and customize the object before returning it.
- More flexible than
service.
Syntax:
Usage:
How It Works:
- The factory function returns the object that will be used as the service instance.
3. Provider
A provider is the most powerful and flexible option. It is used to configure services during the config phase. The provider itself has a .$get method, which returns the service.
Key Characteristics:
- A provider must implement a
$getmethod. - Used when you need to configure a service before it is instantiated.
- Only option available during the config phase.
Syntax:
Usage:
You can configure a provider in the config block.
How It Works:
- You use
providerwhen you need to configure the service before it becomes available.
Comparison Table
| Feature | Service | Factory | Provider |
| What it Returns | An instance of the function. | An object returned by the factory. | Configurable object via $get method. |
| Instantiation | Uses new to instantiate. | Returns an object explicitly. | Uses $get to return the instance. |
| Configurable | No. | No. | Yes, in the config phase. |
| When to Use | For simpler logic or methods. | When you need to construct an object. | When you need pre-instantiation config. |
| Syntax Complexity | Simple | Flexible | Most flexible, but complex. |
When to Use Which?
- Service: Use when you need a simple object or methods attached to
this. - Factory: Use when you need more flexibility and control over object creation.
- Provider: Use when you need to configure the service during the config phase.
Example Summary
Here’s a simple comparison using the same functionality implemented in all three:
Service:
Factory:
Provider:
All three can be injected and used in controllers, but providers offer more flexibility through configuration.
Conclusion
- Use
servicefor simple and straightforward logic. - Use
factorywhen you need more control or to return a complex object. - Use
providerwhen you need configuration before service instantiation.
Understanding these differences helps you choose the right approach for your AngularJS application. 🚀

