Explain the hierarchical injector. How does Angular decide which instance to give?
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
- Draw the tree on paper if you get the chance.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?