Explain the hierarchical injector. How does Angular decide which instance to give?

Angular directives, pipes and DI · Experienced

Model answer

Angular has a tree of injectors. A request for a dependency starts at the element injector of the component and moves up through its parents, and then to the environment injector (route level, then root), until a provider is found. The first match wins, so a provider closer to the component overrides one higher up.

Lazy-loaded routes can have their own environment injector. Modifiers such as optional, self, skipSelf and host change where the search starts or what happens if nothing is found.

Tips

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions