Skip to content
tezvyn:
Menu

Angular's Hierarchical Dependency Injection

Source: angular.devMediumHow cards are made

Angular's Hierarchical Dependency Injection

Angular's DI is a tree of injectors mirroring your components. When a component needs a service, Angular walks up the tree to find the first provider. Use it to scope services to UI branches, but beware: providing at a component level creates a new instance.

Why it exists

Large applications need a way to manage dependencies without creating a single, global bucket of services. Hierarchical dependency injection provides a structured, scalable way to provide and consume services, enabling modularity, state encapsulation, and lazy loading.

The mental model

Imagine your app's component hierarchy as a tree. Angular's DI system creates a parallel tree of 'injectors'. When a component asks for a service, Angular starts a search: it looks at its own injector, then its parent's, then its grandparent's, and so on, all the way to the root. The first injector in the chain that knows how to provide the service wins. It's like scope resolution for variables in a programming language.

How it works

When you declare a dependency in a component's constructor (e.g., constructor(private auth: AuthService)), Angular's injector is responsible for providing an instance. The search begins at the component's own injector. If no provider is registered there (in the component's providers array), the request is forwarded to the parent component's injector. This process continues up the tree until it reaches the application's root injector. If no provider is found anywhere in the hierarchy, Angular throws a NullInjectorError.

When to use it

Use hierarchical injection to scope service instances. For example, provide a service at a parent route component to have all child routes share a single instance of that service, creating state that is isolated to that specific feature area. It's also useful for providing different configurations of the same service to different parts of your application.

When not to use it

Avoid providing services at the component level if you need a true, application-wide singleton. For services that should have only one instance across the entire app, like an authentication service or a user settings service, always use the providedIn: 'root' syntax on the @Injectable() decorator. This ensures it's registered with the root injector and is globally available.

One canonical example

A 'smart' container component might be responsible for a list of items. You can provide a ListStateService directly on this container component. All child 'dumb' presentational components can then inject this service to get data or trigger actions. When the user navigates away and the container component is destroyed, the ListStateService instance and its state are automatically cleaned up with it.

Interview question

When a service is provided within a specific component's providers array, what is the primary outcome?

  • a.Angular will always throw a NullInjectorError if the service is not also provided at the root.
  • b.The service becomes a global singleton, accessible throughout the entire application.
  • c.The service can only be injected by the component itself, and not by any of its child components.
  • d.A new instance of the service is created for that component and its descendants, isolated to that part of the UI.Correct
Why?

Providing a service at the component level creates a new instance of that service, scoped to that component and its children, allowing for isolated state within a UI branch. This differs from `providedIn: 'root'`, which creates an application-wide singleton.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles