Hierarchical and scoped injectors

Intermediate · 20 min

Why providedIn: 'root' isn't the only option, and how a component-level provider creates its own, scoped instance.

Before thisProviders and injection tokens

Decoded Frontend · 20 min

Hierarchical and scoped injectors

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Decoded FrontendReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `providedIn: 'root'` registers a service with the application's root injector and makes it tree-shakable — if nothing ever injects it, it's never included in the production bundle at all.
  • A component's own `providers` array creates a new instance of that service scoped to that component (and its children) — a sibling component gets a completely separate instance.
  • Angular resolves an injection request by walking up the injector tree from where it was requested, using the nearest matching provider it finds — not necessarily the root one.

Not every service should be one single, app-wide instance

providedIn: 'root' has been this roadmap’s default so far, and it’s the right default for most services — one shared instance, available everywhere, for the lifetime of the app. But “one instance app-wide” is a choice, not the only option, and plenty of real services genuinely shouldn’t be shared that broadly. A service tracking which tab is currently active inside a specific TabsComponent has no business being shared across every TabsComponent on the page — each one needs its own.

That’s what the injector hierarchy is for: services can be scoped narrower than the whole app, to exactly the part of the component tree that should share one instance.

providedIn: 'root' and why it enables tree-shaking

@Injectable({ providedIn: 'root' })
export class UserService {}

This registers UserService with the application’s root injector — one shared instance for the entire app, created the first time anything injects it. The providedIn syntax has a second benefit beyond convenience: because the provider registration lives directly on the service’s own @Injectable() decorator rather than in some separate module’s providers array, a bundler can determine, just by checking whether anything actually imports and injects UserService, whether it’s used at all — and if nothing does, the whole class can be dropped from the production bundle. A service registered the old way, in an NgModule’s providers array, gets pulled in whenever that module is imported, whether anything actually injects it or not.

Component-level providers: a new instance, scoped to a subtree

@Component({
  selector: 'app-tabs',
  providers: [TabStateService],
  // ...
})
export class TabsComponent {}

Listing a service in a component’s own providers array does something genuinely different from providedIn: 'root': it tells Angular to create a new instance of that service specifically for this component (and everything nested inside it), rather than reusing whatever root-level instance might also exist. Render three <app-tabs> instances, and you get three separate TabStateService instances — each TabsComponent and its descendants share one instance among themselves, but know nothing of the other two.

This is the direct mechanism behind “a feature’s internal state shouldn’t leak into a sibling feature” — scoping a service to the component that owns that feature is how you guarantee each instance of the feature stays isolated, without the service itself needing any special logic to enforce that isolation.

How a request actually gets resolved

When something injects a token, Angular doesn’t just check one fixed spot — it walks up the injector tree starting from wherever the request originated, checking each level for a provider, and uses the first match it finds. A component nested inside a TabsComponent asking for TabStateService finds the component-level provider before it would ever reach the root injector; a component elsewhere in the app, outside any TabsComponent, resolves straight to whatever’s registered at root (if anything is). This is also why the three providedIn options beyond 'root' — 'platform', 'any', and a component’s own ElementInjector — matter: each changes where in that tree the provider lives, which changes which requests find it first. This lesson’s video builds the hierarchy concept directly with runnable examples; the linked “deeper” video focuses specifically on the providedIn options beyond 'root' if you want the full comparison.

Check yourself

  1. 01Why is `providedIn: 'root'` considered better for tree-shaking than listing a service in an older NgModule's `providers` array?

    `providedIn: 'root'` ties the service's registration directly to the service's own class definition, so a bundler can see — just from whether anything actually imports and injects that class — whether it's ever used, and drop it entirely if not. A service registered only in a module's `providers` array is pulled in whenever that module is, whether or not anything in the app actually injects it.

  2. 02A `TabsComponent` lists `TabStateService` in its own component-level `providers` array. If you render three separate `<app-tabs>` instances on one page, how many `TabStateService` instances exist?

    Three — one per `TabsComponent` instance, each scoped to its own component subtree. That's the entire point of a component-level provider: each instance of the component gets its own private copy of the service, rather than all three sharing one root-level instance.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark