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

RxJS Higher Order Mapping

Learn how higher order mapping operators create and manage inner Observable streams using switchMap(), mergeMap(), concatMap() and exhaustMap().

What is Higher Order Mapping?

A higher order Observable is an Observable whose emitted values are themselves Observables.

Higher order mapping operators take a value from an outer Observable, use that value to create or select an inner Observable, and then control how those inner Observables are subscribed to.

Common operators are switchMap(), mergeMap(), concatMap() and exhaustMap().

Higher Order Mapping Flow

Outer Observable Emits Values
→
Mapping Function Creates Inner Observable
→
Inner Observable Async Operation
→
Flattened Result Subscriber Receives Data

Why Use Higher Order Mapping?

A
Async Operations

Connect one Observable to another asynchronous operation.

S
Search

Handle changing search terms and API requests.

O
API Calls

Start an inner API Observable for each source value.

C
Concurrency Control

Control whether inner operations run, queue, merge or switch.

map() vs Higher Order Mapping

The normal map() operator transforms one value into another value.

Higher order mapping operators transform an outer value into an Observable and then control the resulting inner streams.

Normal map()
const result$ = source$.pipe(

    map(value => value * 2)

);
Higher Order Mapping
const result$ = source$.pipe(

    switchMap(value =>
        getData(value)
    )

);

Outer and Inner Observables

Search Term Outer Value
→
switchMap() Creates Request
→
HTTP Observable Inner Stream
→
Search Result Final Output

1. switchMap()

switchMap() maps each outer value to an inner Observable and switches to the newest inner Observable.

When a new outer value arrives, the previous inner subscription is cancelled.

Basic Example
const search$ = of(
    'Java',
    '.NET',
    'Python'
);

const result$ = search$.pipe(

    switchMap(course =>
        of(course + ' results')
    )

);

result$.subscribe({

    next: result => {

        console.log(result);

    }

});

switchMap() Use Case

switchMap() is especially useful when only the latest request is relevant.

A common example is a search box where the user enters a new search term before the previous request has finished.

Search Example
searchControl.valueChanges.pipe(

    debounceTime(300),

    switchMap(term =>
        this.courseService.search(term)
    )

).subscribe({

    next: courses => {

        this.courses = courses;

    },

    error: error => {

        console.error(error);

    }

});

Search Flow

User types: Java

User changes search: Java Full Stack

Latest search becomes the active request.

2. mergeMap()

mergeMap() maps each outer value to an inner Observable and subscribes to the inner Observables concurrently.

Multiple inner operations can therefore remain active at the same time.

Example
const courseIds$ = of(
    101,
    102,
    103
);

const result$ = courseIds$.pipe(

    mergeMap(id =>
        this.courseService
            .getCourse(id)
    )

);

result$.subscribe({

    next: course => {

        console.log(course);

    }

});

mergeMap() Flow

101 Request A
+
102 Request B
+
103 Request C
→
mergeMap() Concurrent Inner Streams

3. concatMap()

concatMap() maps each outer value to an inner Observable and subscribes to those inner Observables sequentially.

The next inner Observable starts only after the previous inner Observable completes.

Example
const topics$ = of(
    'Introduction',
    'Components',
    'Services'
);

const result$ = topics$.pipe(

    concatMap(topic =>
        this.lessonService
            .saveProgress(topic)
    )

);

result$.subscribe({

    next: response => {

        console.log(response);

    }

});

concatMap() Flow

Topic 1 Start
→
Complete Topic 1 Done
→
Topic 2 Start Next
→
Topic 3 Then Next

4. exhaustMap()

exhaustMap() maps an outer value to an inner Observable and ignores new outer values while the current inner Observable is still active.

It is useful when only one operation should be processed at a time and additional triggers should be ignored until the current operation completes.

Example
submitButtonClicks$.pipe(

    exhaustMap(() =>

        this.enquiryService
            .submitEnquiry()

    )

).subscribe({

    next: response => {

        console.log(response);

    }

});

exhaustMap() Flow

Click 1 Start Request
→
Request Active Other Clicks Ignored
→
Complete Ready Again

Higher Order Mapping Operators Comparison

Operator New Inner Observable Previous Inner Main Behavior
switchMap() Starts latest Previous is cancelled Switch to latest
mergeMap() Starts immediately Previous continues Run concurrently
concatMap() Waits for turn Previous continues Run sequentially
exhaustMap() Ignored while active Continues Ignore new triggers during active work

Which Operator Should You Choose?

S
switchMap()

Use when only the latest inner operation matters.

M
mergeMap()

Use when multiple inner operations may run concurrently.

Q
concatMap()

Use when inner operations must be processed in order.

E
exhaustMap()

Use when new triggers should be ignored during active work.

CIIT Training Institute Example

Suppose a CIIT Training Institute website has a course search box. Every time the user enters a search term, the application needs to request matching courses from an API.

Since only the latest search is important, a switchMap() pipeline is suitable for this type of workflow.

Search Pipeline
searchControl.valueChanges.pipe(

    debounceTime(300),

    filter(term =>
        term.length > 2
    ),

    switchMap(term =>
        this.courseService
            .searchCourses(term)
    )

).subscribe({

    next: courses => {

        this.courses = courses;

    },

    error: error => {

        console.error(error);

    }

});

CIIT Training Institute Course Search

Search: Angular

Search: Angular Full Stack

Latest search request is processed.

Sequential Save Example with concatMap()

Suppose several course-progress updates must be sent to the server in the same order in which they are generated.

concatMap() can be used to queue the inner operations and process them sequentially.

Example
const progress$ = of(
    'Components',
    'Services',
    'Routing'
);

progress$.pipe(

    concatMap(topic =>
        this.progressService
            .save(topic)
    )

).subscribe({

    next: response => {

        console.log(response);

    }

});

Concurrent Operations with mergeMap()

When individual operations do not need to wait for each other, mergeMap() can allow multiple inner Observables to be active concurrently.

The order in which responses arrive should therefore not be assumed to be the same as the source order.

Example
const ids$ = of(
    101,
    102,
    103
);

ids$.pipe(

    mergeMap(id =>
        this.courseService
            .getCourse(id)
    )

).subscribe({

    next: course => {

        console.log(course);

    }

});

Single Active Action with exhaustMap()

Consider a submit button that may be clicked several times while the current request is still running.

exhaustMap() can keep the first active request and ignore additional triggers until it completes.

Example
submitClicks$.pipe(

    exhaustMap(() =>
        this.authService
            .login()
    )

).subscribe({

    next: response => {

        console.log(
            'Login successful'
        );

    },

    error: error => {

        console.error(error);

    }

});

Common Real-World Scenarios

Scenario Suggested Operator Reason
Search as user types switchMap() Latest search is more relevant
Independent requests mergeMap() Operations can run concurrently
Ordered save operations concatMap() Operations should execute sequentially
Submit button exhaustMap() Ignore new triggers while active

Important Points

1. Higher Order Mapping Creates Inner Streams

The mapping function can return an Observable for each outer value.

2. Operators Control Inner Subscriptions

Different operators decide whether inner streams are cancelled, merged, queued or ignored.

3. switchMap() Means Latest

New source values switch the active inner subscription.

4. Operator Choice Depends on Business Requirement

Select the operator according to whether work should run concurrently, sequentially, or only for the latest trigger.

Higher Order Mapping Best Practices

  • Choose switchMap(), mergeMap(), concatMap() or exhaustMap() based on the required concurrency behavior.
  • Do not use switchMap() automatically; make sure cancelling the previous inner operation is acceptable.
  • Use concatMap() when ordering between inner operations matters.
  • Use mergeMap() when concurrent inner operations are acceptable.
  • Use exhaustMap() when repeated triggers should be ignored while one operation is active.
  • Handle errors appropriately inside or around the Observable pipeline according to the required behavior.

Summary

Higher order mapping is used when an Observable value needs to produce or select another Observable.

switchMap() switches to the latest inner Observable, mergeMap() allows concurrent inner operations, concatMap() processes inner Observables sequentially, and exhaustMap() ignores new triggers while an inner Observable is active.

In the next topic, we will learn about Subjects.