DEV Community

Krzysztof Platis
Krzysztof Platis

Posted on • Edited on

3

The importance of order in Angular Dependency Injection providers 🔀

The order of providers in Angular Dependency Injection matters.
If you provide a custom implementation "before" the default implementation is provided, then the default implementation will take precedence. The "last provided" wins. Angular traverses the tree of modules, grabs all providers and flatten them all to one list. In case of conflicts, then the last provided in the list wins.

Angular traverses modules tree, starting from the root module (often named AppModule):

  • first are providers from imported modules (depth-first), e.g. imports: [SomeModule, ...]
  • then providers from static methods of imported modules, e.g. imports: [SomeModule.forRootOrWhateverMethodName(), ...]
  • then providers from the currently analyzed module providers: [...]

If you really feel like buying me a coffee

... then feel free to do it. Many thanks! 🙌

Buy Me A Coffee

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
minh_hong_2f841ae0087262 profile image
Minh Hoàng

Not understanding at all. If you can put more examples and use cases, then will be better.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay