Ch. 4 · Angular

Angular Hierarchical Providers and Service Lifetimes

Angular Hierarchical Providers and Service Lifetimes. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Provider placement determines which injector creates a service and which descendants share it. Local providers can deliberately create isolated instances.

Before you start

You should understand a component’s class, template and dependencies. Follow both the framework lifecycle and the ownership of each value or resource. Apply examples inside an Angular application; the surrounding application configuration and imports depend on the feature being demonstrated.

The practical goal is to reason through this situation: Two editor components each providing DraftService receive independent drafts. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Choose the intended lifetime

A session should be shared application-wide; a local draft may need one instance per editor.

Step 2: Place the provider accordingly

Application-level provision shares one service within that injector scope. Component providers create descendant-local instances.

Step 3: Detect accidental duplication

Log instance identity in two consumers. A local provider can shadow a supposedly global service without changing its class name.

Worked scenario

Two editor components each providing DraftService receive independent drafts.

Editor A and editor B each providing DraftService get independent drafts. Moving the provider to their shared owner changes that relationship. This is a design decision: isolated drafts avoid interference, while a shared session enables coordinated state across routes. Provider placement determines behavior, not just injection convenience.

Common mistake

Providing a supposedly global service locally silently creates another instance.

Verify the behavior

Compare service identities across siblings, descendants and route changes. Confirm state persistence matches the intended ownership boundary.

Interview exercise

Share a session across routes.

Answer and reasoning

Place the provider in the application-level environment when one shared lifetime is intended; avoid duplicate component providers.

Continue learning

Compare the scenario with the Angular interview questions and test your understanding with the Angular MCQs. For terminology and implementation details, consult the reference material.

More in Angular

read ✓Angular · mid

Angular Defer Blocks and Lazy Rendering

Load template regions on demand with @defer, choose a trigger, and use placeholders without delaying critical content.

~2 min readread →
read ✓Angular · hard

Angular Global Error Handling

Catch uncaught errors with a custom ErrorHandler, distinguish client from HTTP errors, and surface safe messages to users.

~2 min readread →
read ✓Angular · mid

Angular Host Directives and Composition

Attach behavior to elements with attribute directives, bind to the host with the host property, and compose directives with hostDirectives.

~2 min readread →
esc