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.
Practice
providedIn property to 'root' mean in Angular?Solution
Step 1: Understand
This setting makes Angular create one instance of the service for the whole app.providedIn: 'root'meaningStep 2: Compare with other scopes
Unlike module or component scopes, 'root' shares the same instance everywhere.Final Answer:
The service is a singleton shared across the entire application. -> Option AQuick Check:
Service scope 'root' = singleton app-wide [OK]
- Thinking 'root' creates multiple instances
- Confusing 'root' with component scope
- Assuming 'root' limits service to one module
Solution
Step 1: Recall how to scope service to a module
Angular does not supportprovidedIn: 'module'. Instead, you add the service to the module'sprovidersarray.Step 2: Check other options
@Injectable({ providedIn: 'root' }) scopes service app-wide, Add the service to the component'sprovidersarray scopes to component, @Injectable({ providedIn: 'module' }) is invalid syntax.Final Answer:
Add the service to the module'sprovidersarray -> Option AQuick Check:
Module scope = add to module providers [OK]
- Using 'providedIn: module' which is invalid
- Confusing component providers with module providers
- Assuming 'root' scopes service to module only
@Component({
selector: 'app-example',
template: `{{counter}}
`,
providers: [CounterService]
})
export class ExampleComponent {
counter = 0;
constructor(private counterService: CounterService) {
this.counter = this.counterService.increment();
}
}If two instances of
app-example are rendered, what will be the value of counter in each?Solution
Step 1: Understand component-level service scope
ProvidingCounterServiceinprovidersinside the component creates a new instance per component.Step 2: Analyze two component instances
Each component instance gets its own service instance, so each counter starts fresh and increments once to 1.Final Answer:
Both will show 1 because each has its own service instance. -> Option CQuick Check:
Component scope = separate service instances per component [OK]
- Assuming service is shared globally despite component providers
- Thinking service is not provided at all
- Confusing component scope with root scope
providers array. However, when injecting it in a lazy-loaded module, Angular throws an error that the service is not found. What is the likely cause?Solution
Step 1: Understand lazy-loaded module injectors
Lazy-loaded modules have their own injectors and do not inherit providers from eagerly loaded modules.Step 2: Identify provider visibility issue
Since the service is provided in a feature module not loaded by the lazy module, the lazy module injector can't find it.Final Answer:
The lazy-loaded module has its own injector and does not see the feature module's providers. -> Option BQuick Check:
Lazy modules have separate injectors = missing providers [OK]
- Assuming root injector shares providers with lazy modules
- Forgetting to add @Injectable decorator
- Confusing export with provider visibility
Solution
Step 1: Separate logs per component instance
Providing the logging service in each component'sproviderscreates a new instance per component, isolating logs.Step 2: Share global settings
Injecting a global settings service provided inrootallows sharing common configuration across all logging instances.Final Answer:
Provide the service in each component'sprovidersarray and inject a global settings service fromroot. -> Option DQuick Check:
Component scope for logs + root scope for settings = best mix [OK]
- Using root scope for logs causes shared state issues
- Providing only in module loses per-component isolation
- Using static variables breaks instance separation
Ready to prove it?
Pass the test and this pattern moves unknown
