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 Subscription

Learn how to subscribe to an Observable, receive emitted values, handle errors and completion, and work with active subscriptions in Angular applications.

What is a Subscription?

A Subscription represents an active execution of an Observable.

When we call subscribe(), the application starts listening to values emitted by the Observable.

The returned Subscription object can also be used to stop the Observable execution when required.

Subscription Flow

Observable Data Source
→
subscribe() Start Listening
→
next() Receive Value
→
Subscription Active Execution

Basic subscribe() Syntax

TypeScript
observable.subscribe({
    next: value => {
        console.log(value);
    },
    error: error => {
        console.error(error);
    },
    complete: () => {
        console.log('Completed');
    }
});

Observer Callbacks

N
next

Runs whenever the Observable emits a value.

E
error

Runs when the Observable reports an error.

C
complete

Runs when the Observable completes successfully.

S
Subscription

Represents the active execution created by subscribe().

Simple Subscription Example

Consider an Observable that emits three course categories.

Observable
const courses$ =
    new Observable<string>(subscriber => {

        subscriber.next('Java Full Stack');
        subscriber.next('.NET Full Stack');
        subscriber.next('Python Full Stack');

        subscriber.complete();
    });
Subscribe to Observable
courses$.subscribe({

    next: course => {

        console.log(course);

    },

    complete: () => {

        console.log('Course stream completed');

    }

});

Console Output

Java Full Stack

.NET Full Stack

Python Full Stack

Course stream completed

Storing the Subscription

The subscribe() method returns a Subscription object.

We can store that object in a variable when the application needs to manage the subscription later.

Example
const courseSubscription =
    courses$.subscribe({

        next: course => {

            console.log(course);

        }

    });

Subscription Object

A
Active

Represents whether the subscription is still active.

U
unsubscribe()

Stops the subscription when it is no longer required.

S
Subscription

Holds the execution created by subscribe().

M
Multiple

An application can have multiple active subscriptions.

Handling Errors in Subscription

The error callback is used when an Observable reports an error.

Error handling helps the application respond gracefully instead of ignoring failures.

Error Handling Example
courseService.getCourses()
    .subscribe({

        next: courses => {

            console.log(courses);

        },

        error: error => {

            console.error(
                'Unable to load courses',
                error
            );

        }

    });

Handling Completion

The complete callback executes when the Observable finishes successfully.

It can be used for actions that should happen after the complete data stream has finished.

Completion Example
courses$.subscribe({

    next: course => {

        console.log(course);

    },

    complete: () => {

        console.log(
            'All courses received'
        );

    }

});

Subscription with HttpClient

Angular HttpClient methods commonly return Observables.

The application can subscribe to the returned Observable to receive the HTTP response.

Service
getCourses() {

    return this.http.get<Course[]>(
        '/api/courses'
    );

}
Component
this.courseService
    .getCourses()
    .subscribe({

        next: courses => {

            this.courses = courses;

        },

        error: error => {

            console.error(error);

        }

    });

CIIT Training Institute Example

Suppose the CIIT Training Institute dashboard receives course enquiry notifications.

The dashboard can subscribe to an Observable and process each new enquiry whenever it is emitted.

Enquiry Subscription
const enquirySubscription =
    enquiryService.getEnquiries()
        .subscribe({

            next: enquiry => {

                console.log(
                    'New enquiry:',
                    enquiry
                );

            },

            error: error => {

                console.error(error);

            }

        });

CIIT Enquiry Dashboard

New enquiry received

Course: Java Full Stack

Course: .NET Full Stack

Subscription is receiving updates...

Multiple Subscriptions

A component may subscribe to more than one Observable when different data sources are required.

Example
const coursesSubscription =
    courses$.subscribe({

        next: course => {

            console.log(course);

        }

    });


const enquiriesSubscription =
    enquiry$.subscribe({

        next: enquiry => {

            console.log(enquiry);

        }

    });

When multiple subscriptions are created, they should be managed carefully according to how long they need to remain active.

Subscription Lifecycle

Create Observable Data Source
→
subscribe() Start Execution
→
Receive Values next()
→
Complete / Unsubscribe End Execution

Observable vs Subscription

Feature Observable Subscription
Purpose Represents a data stream Represents an active execution
Created By Observable source subscribe()
Receives Values Emits values Receives emitted values
Management Defines the stream Can be stopped using unsubscribe()
Multiple Consumers Possible Each subscribe() creates an execution

Subscription Best Practices

  • Subscribe only when the application actually needs the Observable values.
  • Handle errors where appropriate instead of silently ignoring failures.
  • Keep subscription logic easy to understand and maintain.
  • Manage long-lived subscriptions carefully.
  • Avoid keeping subscriptions active after they are no longer required.
  • Use suitable Angular or RxJS patterns for managing asynchronous streams.

Important Points

1. Calling subscribe() starts listening to an Observable.

2. The returned object is a Subscription.

3. next handles emitted values.

4. error handles Observable errors.

5. complete runs when the Observable completes successfully.

Summary

A Subscription represents an active execution of an Observable.

The subscribe() method is used to receive values through callbacks such as next, error and complete.

Subscriptions should be managed carefully, especially for long-lived streams. In the next topic, we will learn about Data Streams.