All topics
Servicesintermediate

providedIn Strategies

Compare providedIn: 'root', 'platform', 'any', and explicit module/component providers.

providedIn is the metadata property on @Injectable() that tells Angular where in the injector hierarchy a service should be registered, and it's also what enables tree-shaking — a service that's never actually injected anywhere in the final build can be dropped entirely, which wasn't possible with the older pattern of listing providers explicitly in an NgModule's providers array, since the bundler couldn't safely prove those services were unused.

providedIn: 'root' is like one shared company printer everyone in the building uses; providedIn: 'any' is like giving each satellite office its own printer instead, while still following the same shared company purchasing policy.

Key Concepts

1
providedIn: 'root' is the default and most common choice: it registers the service as a singleton at the application's root injector, meaning every part of the app that injects it shares the exact same instance, and — combined with tree-shaking — the service's code isn't included in the bundle at all if nothing ever injects it.
providedIn: 'root'
2
providedIn: 'platform' registers the service at a level above the application injector, shared across multiple Angular applications running on the same page (a rare multi-app-per-page scenario), while providedIn: 'any' gives you one instance per lazily-loaded module that injects it — eagerly-loaded parts of the app share the root instance, but each lazy-loaded module gets its own separate instance, which is a subtle and interview-relevant distinction from plain 'root'.
providedIn: 'platform'providedIn: 'any''root'
3
The alternative to any providedIn string is registering a service explicitly in a component's providers array (or, historically, an NgModule's providers array), which is the right choice specifically when you want a scoped instance tied to part of the component tree rather than a single global instance — tying this back directly to the Hierarchical Injectors topic.
providedInprovidersNgModule