Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dependency Injection Strategies | Section
Advanced Angular Patterns

bookDependency Injection Strategies

Swipe to show menu

Before exploring advanced dependency injection (DI) patterns, you need to recall how Angular's dependency injection (DI) system and hierarchical injectors operate. Angular uses a powerful DI framework to manage service creation and injection. At its core, the DI system allows you to declare dependencies in constructors, and Angular resolves these dependencies at runtime.

The injector hierarchy is key:

  • The root injector is created at application bootstrap and provides services registered with the providedIn: 'root' option or in the root module;
  • Each module, component, and directive can also have its own injector, forming a tree structure;
  • When Angular resolves a dependency, it starts at the requesting component's injector and traverses up the hierarchy until it finds a provider or throws an error if none is found.

This system enables flexible service scoping and overrides, supporting both singleton and localized service instances.

app/services/root-logger.service.ts

app/services/root-logger.service.ts

app/logger.module.ts

app/logger.module.ts

app/services/module-logger.service.ts

app/services/module-logger.service.ts

app/components/component-logger/component-logger.component.ts

app/components/component-logger/component-logger.component.ts

app/services/component-logger.service.ts

app/services/component-logger.service.ts

copy

When you provide services at different levelsโ€”root, module, or componentโ€”you are leveraging Angular's hierarchical injectors to control service scope. Services provided at the root are singletons throughout the app. Module-level providers create a new instance for consumers within that module and its children, while component-level providers ensure a unique instance per component instance. This flexibility is crucial for advanced scenarios.

Multi-provider tokens and factory providers further expand your DI options:

  • Multi-provider tokens let you associate multiple providers with a single injection token, useful for plugin systems or registering multiple strategies;
  • Factory providers allow you to customize how a service is created, even injecting dependencies dynamically based on runtime conditions.

Both patterns interact with the DI hierarchy: multi-providers aggregate values up the injector tree, while factory providers can be scoped at any injector level, giving you fine-grained control over instantiation and configuration.

question mark

You are building a feature module that needs a logging service with state that should be shared only among components in that module, but not with the rest of the application.

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 1. Chapterย 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Sectionย 1. Chapterย 2
some-alt