Services in Angular help share data and logic. Service scope decides where the service lives and who can use it.
Service scope (root, module, component) in Angular
@Injectable({ providedIn: 'root' })
@Injectable({ providedIn: SomeModule })
@Injectable({ providedIn: SomeComponent })providedIn: 'root' means the service is a singleton for the whole app.
You can also provide services in modules or components to limit their scope.
@Injectable({ providedIn: 'root' })
export class UserService { }@Injectable({ providedIn: AdminModule })
export class AdminService { }@Injectable({ providedIn: SomeComponent })
export class LocalService { }This example shows a service provided at root and another provided in a feature module. The FeatureComponent uses both services. The root service is shared app-wide, while the feature service is limited to the feature module.
import { Component, Injectable, NgModule } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class RootService { value = 'root'; } @Injectable() export class FeatureService { value = 'feature'; } @Component({ selector: 'app-feature', template: `Feature: {{ featureService.value }} | Root: {{ rootService.value }}` }) export class FeatureComponent { constructor(public featureService: FeatureService, public rootService: RootService) {} } @NgModule({ declarations: [FeatureComponent], providers: [FeatureService], exports: [FeatureComponent] }) export class FeatureModule {} @Component({ selector: 'app-root', template: `<app-feature></app-feature> Root: {{ rootService.value }}` }) export class AppComponent { constructor(public rootService: RootService) {} }
Services provided in 'root' are singletons and live as long as the app runs.
Providing a service in a module or component creates a new instance limited to that scope.
Use component scope to isolate service instances for specific UI parts.
Service scope controls where and how long a service instance lives.
Use 'root' for app-wide singletons, modules for feature-specific services, and components for isolated instances.
Choosing the right scope helps manage memory and service sharing effectively.