← Back to Angular, page 6

angular-0109 · Easy fresher

What is the difference between providedIn root and providing a service in a component, and how does that affect how many instances exist?

A service with providedIn root has one shared instance for the whole app. A service listed in a component's providers gets a new instance for each component instance, and its children share it. Use root for shared state and component providers for state that belongs to one component.

Full answer

Dependency injection decides where an instance lives. The providedIn and providers settings choose the scope.

@Injectable({ providedIn: 'root' }) registers the service in the root injector. Angular creates it the first time it is injected, and every part of the app receives the same object. This makes it a singleton, and unused services are removed from the bundle (tree-shakable). It is the default choice for shared data such as the logged in user or a cart.

providers: [CounterService] on a component registers it in that component's element injector. Angular creates a new instance for each instance of the component, and all children of that component see the same one. When the component is destroyed, the service is destroyed too, and its ngOnDestroy runs. This fits state that belongs to one widget, for example the state of one table, or a form wizard, where two copies on the page must not share data.

Other places:

  • A route's providers array creates an instance that lives as long as that route (an environment injector for the route and its children).
  • viewProviders is like providers but is not visible to projected content.

A common bug is providing a service in both places by habit: the component then gets its own copy, and shared data seems to "disappear". Another bug is adding a service to the providers of a component that is rendered several times and expecting shared state.

Test question: with providedIn: 'root', two components that inject the service share the same count. With a component-level provider, two instances of the same component show separate counts.

Prefer the narrowest scope that gives the right sharing, and use providedIn: 'root' when you need one shared instance.

typescript
import { Component, Injectable, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class SharedCounter {
  count = 0;
}

@Injectable()
export class LocalCounter {
  count = 0;
}

@Component({
  selector: 'app-counter-box',
  providers: [LocalCounter], // new LocalCounter for every <app-counter-box>
  template: `
    <button (click)="bump()">Bump</button>
    local {{ local.count }}, shared {{ shared.count }}
  `,
})
export class CounterBoxComponent {
  protected readonly local = inject(LocalCounter);
  protected readonly shared = inject(SharedCounter);

  bump(): void {
    this.local.count++;
    this.shared.count++;
  }
}

@Component({
  selector: 'app-two-boxes',
  imports: [CounterBoxComponent],
  template: `<app-counter-box /><app-counter-box />`,
})
export class TwoBoxesComponent {}

Angular 20.3, strict template check passes. A Karma test shows that two component instances have separate counters while the root service count is shared.

Key points

  • `providedIn: 'root'` means one shared, tree-shakable instance.
  • `providers` on a component means one instance per component instance, shared with its children.
  • Route providers live as long as the route.
  • Providing in two places creates two instances.

Common mistakes

  • Adding a root service to component `providers` and losing shared state.
  • Expecting shared state from a component-level service on a repeated component.
  • Not realising the instance is destroyed with the component.

Follow-ups

  • How can you check which instance a component got?
  • What is the difference between `providers` and `viewProviders`?
  • When is a route-level provider better than root?

Tips

  • Use a counter example with two components.
  • Mention tree-shaking for root services.
  • Say that you choose the narrowest scope that works.

Evaluation rubric

  • Explains root and component scope correctly.
  • Mentions route providers and lifetime.
  • Recognises the duplicate provider bug.