Angular Service Scope
Angular Service Scope determines where a service instance is available and how widely that service can be shared across an Angular application.
What is Service Scope?
Service scope defines the injector level at which a service is provided and therefore determines which components or parts of the application can access that service.
Choosing an appropriate scope helps control service lifetime, sharing behavior, and application structure.
Simple Definition
Service scope answers the question: Where should this service be available?
Angular Service Scope Flow
Common Service Scopes
Root Scope
Service is provided at the application root level.
providedIn: 'root'
Component Scope
Service is provided specifically for a component subtree.
providers: []
Child Scope
Child components can receive a service instance from a parent injector.
Hierarchical
Injector Scope
Angular's hierarchical injectors control service instances.
Injector
1. Root-Level Service
A service configured with providedIn: 'root' is registered with the application's root injector.
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TrainingService {
getInstituteName(): string {
return 'CIIT Training Institute';
}
}
What This Means
The service is available through the root injector and can be injected into components throughout the application.
2. Root Service Sharing
3. Component-Level Service
A service can also be provided at the component level. In that case, Angular can create an instance associated with that component's injector hierarchy.
import { Component } from '@angular/core';
import { CourseStateService }
from './course-state.service';
@Component({
selector: 'app-dashboard',
providers: [
CourseStateService
],
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
constructor(
private courseStateService: CourseStateService
) {
}
}
Important
Providing a service at component level changes the injector context in which that service is resolved.
4. Component-Level Scope Flow
5. Root Scope vs Component Scope
| Root Scope | Component Scope |
|---|---|
| Registered with the root injector. | Registered in a component's provider configuration. |
| Commonly used for application-wide services. | Useful when service state should belong to a component subtree. |
| Components across the application can resolve it. | The service is resolved within the component's injector hierarchy. |
| Common for shared application services. | Useful for isolated component-specific state. |
6. Angular Hierarchical Injection
Angular uses a hierarchical dependency injection system. A service can be resolved through the injector hierarchy that applies to the requesting component.
Application Root
|
├── Dashboard Component
| |
| ├── Course List
| |
| └── Course Details
|
└── Reports Component
Simple Idea
Angular searches the applicable injector hierarchy to resolve a requested service.
7. Isolated Service Instances
Component-level providers can be useful when separate component trees should have their own service state.
import { Injectable } from '@angular/core';
@Injectable()
export class CourseStateService {
selectedCourse = '';
setCourse(course: string): void {
this.selectedCourse = course;
}
}
@Component({
selector: 'app-dashboard',
providers: [
CourseStateService
],
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
constructor(
private courseStateService: CourseStateService
) {
}
}
Use Case
This approach is useful when each component instance or component subtree needs its own service-managed state.
8. Choosing the Right Service Scope
Application Settings
Shared configuration can use application-wide scope.
Root
User Session
Shared session-related state may be maintained by a shared application service.
Shared Service
Local Component State
Component-specific state may use component-level providers.
Component
Feature State
Services can be scoped to the part of the application that needs them.
Feature
9. Service Scope Example
Consider a CIIT Training Institute dashboard where the course catalog is shared application-wide, while a temporary course selection belongs only to one dashboard area.
@Injectable({
providedIn: 'root'
})
export class CourseCatalogService {
getCourses(): string[] {
return [
'Java Full Stack',
'Python Full Stack',
'Full Stack .NET',
'Data Analytics'
];
}
}
@Injectable()
export class DashboardSelectionService {
selectedCourse = '';
}
@Component({
selector: 'app-dashboard',
providers: [
DashboardSelectionService
],
templateUrl: './dashboard.component.html'
})
export class DashboardComponent {
constructor(
private selectionService:
DashboardSelectionService
) {
}
}
CourseCatalogService: Application-wide shared service
DashboardSelectionService: Dashboard-specific service state
Complete CIIT Training Institute Example
Suppose CIIT Training Institute has a common course catalog that should be available throughout the application. At the same time, a dashboard can maintain its own temporary selected-course state.
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CourseCatalogService {
getCourses(): string[] {
return [
'Java Full Stack',
'Python Full Stack',
'Full Stack .NET',
'Data Analytics'
];
}
}
import { Injectable } from '@angular/core';
@Injectable()
export class DashboardSelectionService {
selectedCourse = '';
setCourse(course: string): void {
this.selectedCourse = course;
}
getCourse(): string {
return this.selectedCourse;
}
}
import { Component, OnInit } from '@angular/core';
import { CourseCatalogService }
from './course-catalog.service';
import { DashboardSelectionService }
from './dashboard-selection.service';
@Component({
selector: 'app-dashboard',
providers: [
DashboardSelectionService
],
templateUrl: './dashboard.component.html'
})
export class DashboardComponent implements OnInit {
courses: string[] = [];
selectedCourse = '';
constructor(
private courseCatalogService:
CourseCatalogService,
private dashboardSelectionService:
DashboardSelectionService
) {
}
ngOnInit(): void {
this.courses =
this.courseCatalogService.getCourses();
}
selectCourse(course: string): void {
this.dashboardSelectionService
.setCourse(course);
this.selectedCourse =
this.dashboardSelectionService
.getCourse();
}
}
<h2>
CIIT Training Institute
</h2>
<h4>
Available Courses
</h4>
<ul>
<li *ngFor="let course of courses">
<button
class="btn btn-primary btn-sm mb-2"
(click)="selectCourse(course)">
{{ course }}
</button>
</li>
</ul>
<p>
Selected Course:
{{ selectedCourse }}
</p>
CIIT Training Institute
Available Courses
- Java Full Stack
- Python Full Stack
- Full Stack .NET
- Data Analytics
Selected Course: Java Full Stack
Service Scope Quick Reference
| Scope | Configuration | Typical Use |
|---|---|---|
| Root |
providedIn: 'root'
|
Application-wide services |
| Component |
providers: []
|
Component or subtree-specific state |
| Child Injector | Hierarchical provider configuration | More localized service availability |
Important Points About Service Scope
- Service scope determines where a service can be resolved.
- providedIn: 'root' is commonly used for application-wide services.
- Component providers can create a service scope associated with a component hierarchy.
- Angular uses hierarchical dependency injection.
- The scope should match the responsibility and sharing requirements of the service.
- Service scope can affect how service state is shared between components.
Service Scope Best Practices
- Use root scope for genuinely application-wide services.
- Use component-level providers when state should remain associated with a component subtree.
- Avoid using a broad scope when a service only needs a narrow responsibility.
- Choose scope based on how the data and functionality need to be shared.
- Keep service responsibilities clear and focused.
- Understand the injector hierarchy before choosing a custom service scope.
Summary
Angular Service Scope determines where a service is provided and how its instance can be resolved through the dependency injection hierarchy.
Root-level services are commonly used for shared application functionality, while component-level providers are useful for localized or component-specific service state.