DEV Community

Krzysztof Platis
Krzysztof Platis

Posted on • Edited on

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

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.