Angular › Dependency Injection Providers & Injection Tokens
What does providedIn root mean when registering an Angular service?
BeginnerAnswer
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.