Skip to main content

Hierarchical Injectors, Singleton Services and inject()

Learn Hierarchical Injectors, Singleton Services and inject() (Phase 5 · Services and Dependency Injection (Essential), Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

The injector tree • Angular has a hierarchy of injectors : the environment injector tree (root, route-level, set by providers ) and the element injector tree (one per component/directive in the DOM). A lookup starts at the requesting component and walks up until a provider is found, else throws NullInjectorError . • Where you provide a service decides its scope and lifetime : root = one instance for the whole app; route providers = one per lazy route; component providers = a new instance for each component instance (destroyed with it).

More lessons in Phase 5 · Services and Dependency Injection (Essential)