Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

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

Service Reusable class
→
Provider Defines scope
→
Injector Creates instance
→
Components Use service

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.

training.service.ts
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

TrainingService Root Provider
→
Course List Injects Service
→
Dashboard Injects Service
→
Reports Injects Service

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.

dashboard.component.ts
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

Parent Component Provides service
→
Service Instance Created for scope
→
Child Components Can resolve service

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.

Example Structure
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.

course-state.service.ts
import { Injectable } from '@angular/core';

@Injectable()
export class CourseStateService {

    selectedCourse = '';

    setCourse(course: string): void {

        this.selectedCourse = course;

    }

}
dashboard.component.ts
@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.

Application-Wide Service
@Injectable({
    providedIn: 'root'
})
export class CourseCatalogService {

    getCourses(): string[] {

        return [
            'Java Full Stack',
            'Python Full Stack',
            'Full Stack .NET',
            'Data Analytics'
        ];

    }

}
Component-Scoped Service
@Injectable()
export class DashboardSelectionService {

    selectedCourse = '';

}

@Component({

    selector: 'app-dashboard',

    providers: [
        DashboardSelectionService
    ],

    templateUrl: './dashboard.component.html'

})
export class DashboardComponent {

    constructor(
        private selectionService:
            DashboardSelectionService
    ) {

    }

}
Scope Example:

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.

course-catalog.service.ts
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'

        ];

    }

}
dashboard-selection.service.ts
import { Injectable } from '@angular/core';

@Injectable()
export class DashboardSelectionService {

    selectedCourse = '';

    setCourse(course: string): void {

        this.selectedCourse = course;

    }

    getCourse(): string {

        return this.selectedCourse;

    }

}
dashboard.component.ts
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();

    }

}
dashboard.component.html
<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>
Expected Output:

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.