Advertisement
Beginner Angular, Dependency Injection Providers & Injection Tokens

What does providedIn root mean when registering an Angular service?

Short answer

Setting providedIn to root tells Angular to make this service available application wide, using a single shared instance, without needing to manually list it in any component's or module's providers array.

Save Angular mock interview
Advertisement
AModel answer
Setting providedIn to root tells Angular to make this service available application wide, using a single shared instance, without needing to manually list it in any component's or module's providers array. This is the most common and simplest way to register a service in modern Angular.
@Injectable({ providedIn: 'root' })
export class CartService {
  items: string[] = [];
}

// Now injectable anywhere in the app without any extra setup
constructor(private cartService: CartService) {}
Real-world example
A shopping cart service registered with providedIn root is automatically available to every component across the entire app, sharing the exact same cart data everywhere it is injected.
Likely follow-ups
  • What is the benefit of providedIn root over manually adding a service to a providers array?
  • How would you provide a service only for a specific feature area instead of the entire app?
Related topics