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

Sharing Data Between Components

Angular Services can be used to share data between different components. A shared service can store information and provide methods that multiple components can use.

What Does Sharing Data Mean?

In Angular applications, different components may need access to the same information.

Instead of keeping duplicate copies of the same data in multiple components, a service can maintain the shared information and provide methods for reading and updating it.

Simple Definition

Data sharing through a service means storing common information in a reusable service so that multiple components can access it.

Sharing Data Flow

Component A Updates data
→
Shared Service Stores data
→
Component B Reads data

Why Share Data Through a Service?

Common Data

Keep information that is required by multiple components in one place.

Shared State
Reusable

Different components can reuse the same service.

Reuse
Centralized Data

Keep related data management inside a single service.

Centralized
Clean Components

Components can focus on their own UI responsibilities.

Separation

1. Creating a Shared Data Service

First, create a service that contains the data which needs to be shared.

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

@Injectable({
    providedIn: 'root'
})
export class SharedCourseService {

    selectedCourse = '';

    setCourse(course: string): void {

        this.selectedCourse = course;

    }

    getCourse(): string {

        return this.selectedCourse;

    }

}
How it works

The service keeps the selected course in one common property. One component can update it using setCourse() and another component can read it using getCourse().

2. Updating Shared Data from Component A

The first component can update the shared data through the service.

course-list.component.ts
import { Component } from '@angular/core';
import { SharedCourseService }
from './shared-course.service';

@Component({
    selector: 'app-course-list',
    templateUrl: './course-list.component.html'
})
export class CourseListComponent {

    constructor(
        private sharedCourseService: SharedCourseService
    ) {

    }

    selectCourse(course: string): void {

        this.sharedCourseService.setCourse(course);

    }

}
course-list.component.html
<button
    class="btn btn-primary"
    (click)="selectCourse('Java Full Stack')">

    Select Java Full Stack

</button>
Expected Behavior:

The selected course is stored inside the shared service.

3. Reading Shared Data in Component B

Another component can use the same service to read the shared data.

course-details.component.ts
import { Component, OnInit } from '@angular/core';
import { SharedCourseService }
from './shared-course.service';

@Component({
    selector: 'app-course-details',
    templateUrl: './course-details.component.html'
})
export class CourseDetailsComponent implements OnInit {

    selectedCourse = '';

    constructor(
        private sharedCourseService: SharedCourseService
    ) {

    }

    ngOnInit(): void {

        this.selectedCourse =
            this.sharedCourseService.getCourse();

    }

}
course-details.component.html
<h3>
    Selected Course
</h3>

<p>
    {{ selectedCourse }}
</p>
Expected Output:
Selected Course: Java Full Stack

4. Complete Shared Data Flow

Course List Select Course
→
Shared Service Store Course
→
Course Details Read Course
→
Template Display Course

5. Sharing Data with Multiple Components

The same shared service can be injected into multiple components.

Shared Service
@Injectable({
    providedIn: 'root'
})
export class TrainingService {

    selectedCourse = '';

    setCourse(course: string): void {

        this.selectedCourse = course;

    }

    getCourse(): string {

        return this.selectedCourse;

    }

}
Course List Updates course
→
TrainingService Shared data
→
Dashboard Reads course
→
Notification Uses course

6. Sharing Multiple Values

A service can store more than one value when multiple pieces of application information need to be shared.

training.service.ts
@Injectable({
    providedIn: 'root'
})
export class TrainingService {

    selectedCourse = '';

    trainingMode = '';

    trainerName = '';

    setDetails(
        course: string,
        mode: string,
        trainer: string
    ): void {

        this.selectedCourse = course;

        this.trainingMode = mode;

        this.trainerName = trainer;

    }

    getDetails(): any {

        return {

            course: this.selectedCourse,

            mode: this.trainingMode,

            trainer: this.trainerName

        };

    }

}
Example Shared Information:

Course: Full Stack .NET

Training Mode: Offline

Trainer: CIIT Trainer

7. Sharing Data Dynamically

When components need to react when shared data changes, a service can expose an Observable-based value.

training.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
    providedIn: 'root'
})
export class TrainingService {

    private courseSubject =
        new BehaviorSubject<string>('');

    course$ =
        this.courseSubject.asObservable();

    setCourse(course: string): void {

        this.courseSubject.next(course);

    }

}
Component
ngOnInit(): void {

    this.trainingService.course$
        .subscribe(course => {

            this.selectedCourse = course;

        });

}
How it works

The service publishes the latest selected course. Components subscribed to that shared value can receive updated data.

Data Sharing Approaches

Approach Use Example
Service Property Store common data selectedCourse
Service Methods Update or retrieve shared data setCourse()
Observable Notify components about data changes course$
BehaviorSubject Maintain and emit the latest value BehaviorSubject

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute website has a course selection page and a course summary section. When a user selects a course, another component should be able to read the selected course.

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

@Injectable({
    providedIn: 'root'
})
export class CourseSelectionService {

    private selectedCourse = '';

    setSelectedCourse(course: string): void {

        this.selectedCourse = course;

    }

    getSelectedCourse(): string {

        return this.selectedCourse;

    }

}
course-selection.component.ts
import { Component } from '@angular/core';
import { CourseSelectionService }
from './course-selection.service';

@Component({
    selector: 'app-course-selection',
    templateUrl: './course-selection.component.html'
})
export class CourseSelectionComponent {

    constructor(
        private courseSelectionService:
            CourseSelectionService
    ) {

    }

    selectCourse(course: string): void {

        this.courseSelectionService
            .setSelectedCourse(course);

    }

}
course-selection.component.html
<h3>
    Select a Course
</h3>

<button
    class="btn btn-primary me-2"
    (click)="selectCourse('Java Full Stack')">

    Java Full Stack

</button>

<button
    class="btn btn-primary"
    (click)="selectCourse('Python Full Stack')">

    Python Full Stack

</button>
course-summary.component.ts
import { Component, OnInit } from '@angular/core';
import { CourseSelectionService }
from './course-selection.service';

@Component({
    selector: 'app-course-summary',
    templateUrl: './course-summary.component.html'
})
export class CourseSummaryComponent implements OnInit {

    selectedCourse = '';

    constructor(
        private courseSelectionService:
            CourseSelectionService
    ) {

    }

    ngOnInit(): void {

        this.selectedCourse =
            this.courseSelectionService
                .getSelectedCourse();

    }

}
course-summary.component.html
<h3>
    Course Summary
</h3>

<p>
    Selected Course:
    {{ selectedCourse }}
</p>
Expected Output:

Course Summary

Selected Course: Java Full Stack

Advantages of Sharing Data Through Services

  • Multiple components can access common information.
  • Data management can be centralized in one service.
  • Components become simpler and more focused.
  • Repeated data logic can be avoided.
  • Services can provide reusable methods for reading and updating data.
  • Observable-based services can notify components about changes.

Sharing Data Best Practices

  • Keep shared data related to a clear application responsibility.
  • Use meaningful service and property names.
  • Avoid unnecessary duplication of the same data across components.
  • Use service methods to control how shared data is updated.
  • Use Observable-based patterns when components need to react to changing shared data.
  • Keep business logic inside services instead of spreading it across multiple components.

Summary

Angular Services provide a convenient way to share data between different components by keeping common information inside a reusable service.

Components can update and read shared data through service methods, while Observable-based approaches can be used when components need to react to data changes.