Back to results for “Dependency injection”
Angular › Dependency Injection Providers & Injection Tokens

What is a provider in Angular's dependency injection system?

Beginner

Answer

A provider tells Angular's dependency injection system how to create a specific value or service when something asks for it. Most commonly this is just the class itself, but a provider can also describe using a different class, a fixed value, or a factory function instead.
@Injectable({ providedIn: 'root' })
export class LoggerService {
  log(message: string) { console.log(message); }
}

// The providedIn root option registers this as a provider automatically at the app level
Real-world example A logging service is registered once as a provider at the root of the app, letting any component or service throughout the entire app request and receive the exact same shared instance automatically.

Common follow-ups: What is the difference between providing a service at the root level versus inside a specific component?;What happens if two different providers are registered for the same service at different levels?