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 Unsubscribe

Learn how to stop an Observable subscription when it is no longer required and how subscription management helps maintain efficient Angular applications.

What is Unsubscribe?

Unsubscribe means stopping an active Observable subscription.

When an Observable is subscribed to, the resulting Subscription can remain active until the source completes, errors, or the subscription is explicitly cancelled.

For long-lived streams, proper subscription management helps prevent work from continuing after it is no longer needed.

Subscription and Unsubscribe Flow

Observable Data Stream
→
subscribe() Start Listening
→
Subscription Active
→
unsubscribe() Stop Listening

Why is Unsubscribe Important?

1
Stop Unnecessary Work

Stops an active subscription when the data is no longer required.

2
Manage Long Streams

Useful for streams that can remain active for a long time.

3
Cleaner Components

Helps components stop work that is no longer relevant.

4
Better Resource Management

Properly managed subscriptions can reduce unnecessary ongoing activity.

Basic unsubscribe() Syntax

Store the Subscription returned by subscribe() and call unsubscribe() when the subscription is no longer required.

TypeScript
const subscription =
    data$.subscribe({

        next: value => {

            console.log(value);

        }

    });

subscription.unsubscribe();

Simple Unsubscribe Example

Observable
const numbers$ =
    interval(1000);
Subscribe
const subscription =
    numbers$.subscribe({

        next: value => {

            console.log(value);

        }

    });
Unsubscribe
setTimeout(() => {

    subscription.unsubscribe();

}, 5000);

Example Output

0

1

2

3

Subscription stopped after the specified time.

Complete vs Unsubscribe

An Observable may complete by itself after finishing its sequence of values.

Unsubscribe() is an explicit action used by the consumer to cancel its subscription.

Feature Complete Unsubscribe
Triggered By Observable Subscriber / Application
Meaning Stream has finished successfully Subscription is cancelled
Method complete callback unsubscribe()
Common Use Finite streams Long-lived or no-longer-needed streams

Managing Multiple Subscriptions

An application may create more than one subscription.

Each Subscription can be managed separately, or related subscriptions can be grouped for cleanup.

Example
const courseSubscription =
    courses$.subscribe({

        next: course => {

            console.log(course);

        }

    });


const enquirySubscription =
    enquiries$.subscribe({

        next: enquiry => {

            console.log(enquiry);

        }

    });


courseSubscription.unsubscribe();

enquirySubscription.unsubscribe();

Grouping Subscriptions

RxJS Subscription objects can be grouped so that related subscriptions can be cancelled together.

This is useful when several subscriptions belong to the same component or workflow.

Example
const subscription =
    new Subscription();

subscription.add(
    courses$.subscribe({
        next: course => {
            console.log(course);
        }
    })
);

subscription.add(
    enquiries$.subscribe({
        next: enquiry => {
            console.log(enquiry);
        }
    })
);


subscription.unsubscribe();

CIIT Training Institute Example

Suppose a CIIT Training Institute dashboard subscribes to a continuously updating enquiry stream.

When the dashboard component is no longer using the stream, the subscription should be cleaned up according to the application's subscription-management strategy.

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

            next: enquiry => {

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

            }

        });
Cleanup
enquirySubscription.unsubscribe();

CIIT Enquiry Dashboard

Java Full Stack enquiry received

.NET Full Stack enquiry received

Python Full Stack enquiry received

Live enquiry subscription stopped.

When Should We Unsubscribe?

L
Long-Lived Stream

When the source can continue emitting for a long time.

P
Page / Component Exit

When a component no longer needs an active stream.

C
Cancel Operation

When the application intentionally cancels ongoing work.

S
Stop Monitoring

When continuous updates are no longer required.

Finite and Long-Lived Observables

Type Example Typical Behavior
Finite A stream with a known completion Emits values and then completes
Long-Lived Timer or ongoing event stream May continue until cancelled
User Controlled Start / Stop workflow Subscription can be cancelled explicitly

Angular Subscription Management

Angular provides patterns for managing Observable lifecycles together with component lifecycles.

The exact approach can depend on the Angular version and the application's architecture.

Important Points

1. subscribe() Returns a Subscription

Store the returned Subscription when explicit subscription management is required.

2. unsubscribe() Cancels the Subscription

Calling unsubscribe() stops that subscription from continuing to receive future emissions.

3. Long-Lived Streams Need Attention

Timers, event streams and other ongoing sources should be managed carefully.

4. Choose the Right Lifecycle Strategy

Use an appropriate Angular or RxJS subscription-management approach for the application's requirements.

Unsubscribe Best Practices

  • Identify subscriptions that can remain active for a long time.
  • Cancel subscriptions when their work is no longer required.
  • Keep subscription ownership clear inside the application.
  • Group related subscriptions when that makes cleanup simpler.
  • Avoid unnecessary manual subscription management when a suitable Angular pattern already handles the lifecycle.
  • Review long-lived streams carefully during component design.

Summary

unsubscribe() is used to cancel an active Observable subscription when it is no longer required.

Subscription management is especially important for long-lived streams such as timers, event streams and continuously updating application data.

The Observables module is now complete. In the next module, we will learn about RxJS.