A singleton service means there is only one shared instance of that service in your app. This helps keep data and logic consistent everywhere you use it.
Singleton service behavior in Angular
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class MyService { // service code here }
The providedIn: 'root' option makes Angular create one shared instance for the whole app.
You can inject this service into any component or other service to use the same instance.
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CounterService { count = 0; increment() { this.count++; } }
import { Injectable } from '@angular/core'; @Injectable() export class LocalService { data = 'local'; }
Both CounterAComponent and CounterBComponent use the same CounterService instance. Incrementing in one updates the shared count, so both show the same number.
import { Component, Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CounterService { count = 0; increment() { this.count++; } } @Component({ selector: 'app-counter-a', template: `<button (click)="increment()">Increment A</button> Count A: {{counterService.count}}` }) export class CounterAComponent { constructor(public counterService: CounterService) {} increment() { this.counterService.increment(); } } @Component({ selector: 'app-counter-b', template: `<button (click)="increment()">Increment B</button> Count B: {{counterService.count}}` }) export class CounterBComponent { constructor(public counterService: CounterService) {} increment() { this.counterService.increment(); } } @Component({ selector: 'app-root', template: ` <app-counter-a></app-counter-a> <app-counter-b></app-counter-b> ` }) export class AppComponent {}
If you provide a service in a component's providers array, Angular creates a new instance for that component and its children, breaking singleton behavior.
Singleton services are great for shared state but be careful with mutable data to avoid unexpected changes.
Singleton services have one shared instance across the app.
Use providedIn: 'root' to make a service singleton automatically.
Singletons help share data and logic easily between components.
Practice
singleton?Solution
Step 1: Understand singleton concept in Angular
A singleton service means Angular creates one instance of the service and shares it everywhere.Step 2: Compare options with singleton definition
Only There is only one shared instance of the service across the entire app. correctly describes a single shared instance across the app.Final Answer:
There is only one shared instance of the service across the entire app. -> Option AQuick Check:
Singleton = one shared instance [OK]
- Thinking each component gets its own service instance
- Confusing singleton with service destruction
- Believing singleton limits service usage to one component
Solution
Step 1: Recall Angular service singleton syntax
Angular usesprovidedIn: 'root'to provide a singleton service at the app root level.Step 2: Check each option's validity
Only @Injectable({ providedIn: 'root' }) uses the correct syntax. Others are invalid or do not exist in Angular.Final Answer:
@Injectable({ providedIn: 'root' }) -> Option BQuick Check:
Singleton service = providedIn: 'root' [OK]
- Using non-existent properties like singleton or scope
- Confusing component-level providers with root-level
- Omitting the @Injectable decorator
export class CounterService {
count = 0;
increment() { this.count++; }
getCount() { return this.count; }
}Both components call
increment() once. What will getCount() return when called from either component?Solution
Step 1: Understand singleton service shared state
Since the service is singleton, both components share the samecountvariable.Step 2: Calculate increments from both components
Each component callsincrement()once, so total increments = 2.Final Answer:
2 -> Option CQuick Check:
Shared state increments twice = 2 [OK]
- Assuming each component has separate service instance
- Thinking count resets per component
- Ignoring shared service state
@Injectable() but forgot to add providedIn: 'root'. What problem will occur when multiple components inject this service?Solution
Step 1: Understand default service provider behavior
WithoutprovidedIn: 'root', Angular does not provide the service globally.Step 2: Effect on multiple injections
Each component that provides the service separately will get its own instance, so no singleton.Final Answer:
Angular will create separate instances for each component, breaking singleton behavior. -> Option DQuick Check:
Missing providedIn: 'root' = multiple instances [OK]
- Assuming singleton is default without providedIn
- Expecting compile errors for missing providedIn
- Thinking service becomes unusable
Solution
Step 1: Ensure singleton service provision
UsingprovidedIn: 'root'makes the service singleton app-wide.Step 2: Use BehaviorSubject for reactive shared state
BehaviorSubject allows components to subscribe and get updates when login status changes.Step 3: Evaluate other options
Options B, C, and D break singleton or do not share state properly.Final Answer:
Create service with @Injectable({ providedIn: 'root' }) and use a BehaviorSubject to hold login status. -> Option AQuick Check:
Singleton + reactive state = shared updates [OK]
- Adding service to component providers breaking singleton
- Storing shared data only inside components
- Manually creating service instances instead of DI
Ready to prove it?
Pass the test and this pattern moves unknown
