Angular Providers
Angular Providers tell the Dependency Injection system how a dependency should be created and supplied when a component or service requests it.
What is a Provider?
A provider is a configuration that tells Angular how to obtain a dependency when it is requested through Dependency Injection.
Providers can supply class instances, values, existing services, or values created through factory functions.
Simple Definition
A Provider tells Angular what should be provided when a dependency is requested.
Angular Provider Flow
Common Provider Types
Class Provider
Provides an instance of a class.
useClass
Value Provider
Provides a fixed value or configuration object.
useValue
Factory Provider
Creates a dependency using a factory function.
useFactory
Existing Provider
Reuses an already registered dependency.
useExisting
1. Basic Provider Configuration
A provider can associate a dependency token with the class, value, or factory Angular should use.
providers: [
CourseService
]
How it works
Angular can use the listed service as a dependency provider for the injector associated with that configuration.
2. useClass Provider
The useClass provider tells Angular to use a particular class when a dependency token is requested.
export class CourseService {
getCourse(): string {
return 'Java Full Stack';
}
}
providers: [
{
provide: CourseService,
useClass: CourseService
}
]
Simple Idea
useClass allows Angular to associate a token with the class that should be instantiated.
3. Providing an Alternative Class
One token can be configured to use a different implementation class.
export class CourseService {
getCourse(): string {
return 'Java Full Stack';
}
}
export class AdvancedCourseService {
getCourse(): string {
return 'Advanced Angular Full Stack';
}
}
providers: [
{
provide: CourseService,
useClass: AdvancedCourseService
}
]
Course: Advanced Angular Full Stack
4. useValue Provider
The useValue provider supplies a fixed value directly through Dependency Injection.
providers: [
{
provide: 'API_URL',
useValue: 'https://api.example.com'
}
]
Use Case
useValue is useful for strings, numbers, configuration objects, feature flags, and other fixed values.
5. useValue with Configuration
A configuration object can be provided using useValue.
const appConfig = {
instituteName:
'CIIT Training Institute',
environment:
'production',
enableLogging:
true
};
providers: [
{
provide: APP_CONFIG,
useValue: appConfig
}
]
Institute: CIIT Training Institute
Environment: production
Logging: Enabled
6. useFactory Provider
A useFactory provider uses a function to create or calculate the dependency value.
function createApiUrl(): string {
return 'https://api.example.com';
}
providers: [
{
provide: API_URL,
useFactory: createApiUrl
}
]
How it works
Angular calls the factory function to obtain the dependency value when the provider needs to create it.
7. Factory Provider with Dependencies
A factory provider can itself depend on other registered services or values.
function createMessage(
trainingService: TrainingService
): string {
return trainingService
.getInstituteName();
}
providers: [
{
provide: APP_MESSAGE,
useFactory: createMessage,
deps: [TrainingService]
}
]
Important
The deps array tells Angular which dependencies should be provided to the factory function.
8. useExisting Provider
The useExisting provider creates an alias for an already registered dependency instead of creating another instance.
providers: [
CourseService,
{
provide: TrainingService,
useExisting: CourseService
}
]
Simple Idea
useExisting tells Angular to use an existing instance associated with another provider.
9. Provider Types at a Glance
10. Provider with InjectionToken
InjectionToken can be used together with providers to inject values or configuration objects that do not have a suitable class token.
import { InjectionToken }
from '@angular/core';
export const API_URL =
new InjectionToken<string>('API_URL');
providers: [
{
provide: API_URL,
useValue:
'https://api.example.com'
}
]
Connection
The InjectionToken identifies the dependency, while the Provider tells Angular what value should be associated with that token.
11. Component-Level Providers
Providers can be declared in a component when the dependency should be resolved within that component's injector hierarchy.
@Component({
selector: 'app-dashboard',
providers: [
CourseStateService
],
templateUrl:
'./dashboard.component.html'
})
export class DashboardComponent {
constructor(
private courseStateService:
CourseStateService
) {
}
}
12. Application-Level Providers
Providers can also be configured at application startup so that they are available through the application's injector hierarchy.
import {
ApplicationConfig
} from '@angular/core';
export const appConfig:
ApplicationConfig = {
providers: [
CourseService,
TrainingService
]
};
Use Case
Application-level providers are useful for dependencies that need to be registered during application configuration.
13. provide and Token
The provide property identifies the dependency token that Angular should register.
providers: [
{
provide: API_URL,
useValue:
'https://api.example.com'
}
]
14. Provider Comparison
| Provider | Purpose | Example |
|---|---|---|
| useClass | Provides a class implementation |
useClass: MyService
|
| useValue | Provides a fixed value |
useValue: 'CIIT'
|
| useFactory | Creates dependency using a function |
useFactory: createValue
|
| useExisting | Reuses an existing dependency |
useExisting: ExistingService
|
Complete CIIT Training Institute Example
Suppose the CIIT Training Institute Angular application needs a course service and an application configuration value. Providers can define how both dependencies should be supplied.
import { Injectable }
from '@angular/core';
@Injectable()
export class TrainingService {
getInstituteName(): string {
return 'CIIT Training Institute';
}
}
import { InjectionToken }
from '@angular/core';
export const TRAINING_MODE =
new InjectionToken<string>(
'TRAINING_MODE'
);
export const appConfig:
ApplicationConfig = {
providers: [
TrainingService,
{
provide: TRAINING_MODE,
useValue:
'Online + Offline'
}
]
};
import {
Component,
inject
} from '@angular/core';
import { TrainingService }
from './training.service';
import { TRAINING_MODE }
from './tokens';
@Component({
selector: 'app-dashboard',
templateUrl:
'./dashboard.component.html'
})
export class DashboardComponent {
private trainingService =
inject(TrainingService);
trainingMode =
inject(TRAINING_MODE);
instituteName =
this.trainingService
.getInstituteName();
}
<h2>
{{ instituteName }}
</h2>
<p>
Training Mode:
{{ trainingMode }}
</p>
CIIT Training Institute
Training Mode: Online + Offline
15. Choosing the Right Provider
UseClass
Use when Angular should provide a class implementation.
useClass
UseValue
Use for fixed values and application configuration.
useValue
UseFactory
Use when the dependency needs to be created dynamically.
useFactory
UseExisting
Use when an existing service instance should be reused.
useExisting
Important Points About Providers
- Providers define how Angular should supply dependencies.
- useClass provides a class implementation.
- useValue provides a fixed value or object.
- useFactory creates a dependency using a function.
- useExisting reuses an already registered dependency.
- Providers can be configured at different levels of the Angular injector hierarchy.
Provider Best Practices
- Choose the provider type according to the dependency requirement.
- Use meaningful InjectionToken names for value-based dependencies.
- Keep provider configuration easy to read and maintain.
- Use application-wide providers only when broad availability is actually required.
- Use component-level providers when localized service state is appropriate.
- Avoid unnecessary duplicate service instances.
Summary
Angular Providers define how dependencies should be created, supplied, or reused by the Dependency Injection system.
The important provider strategies are useClass, useValue, useFactory, and useExisting.