RxJS operations in Angular
Last Updated :
02 May, 2024
Angular, a popular front-end framework, helps to build responsive and scalable applications. At the core of Angular lies RxJS (Reactive Extensions for JavaScript), a powerful library for handling asynchronous operations and event-based programming. In this article, we'll explore how RxJS helps Angular developers manage complex asynchronous tasks and streamline data flow within their applications.
Prerequisites:
What are RxJS Operations?
In Angular applications, RxJS (Reactive Extensions for JavaScript) is a powerful library used for handling asynchronous and event-based programming using observables. RxJS provides a wide range of operators that enable you to manipulate, transform, combine, and manage observables in a flexible and functional way. These operators make it easier to work with data streams and asynchronous operations in a reactive manner.
Uses of RxJS operations
- Allows the users to map function to transform the value as given by the observable.
- Used to collect and combine various observables into one observable.
- Used to determine the number of values that will be emitted by the observable.
- Used to tackle or handle the error which is emitted by the observable.
- Used to filter the data on the basis of some function.
- Used to determine the time when the values will be emitted by the observables.
Common RxJS Operations in Angular
- Observable Creation:
of()
: Creates an observable that emits a sequence of values.from()
: Converts an array, promise, or iterable into an observable.ajax()
: Performs an XMLHttpRequest with the provided options and returns an observable.
- Combination Operators:
merge()
: Combines multiple observables into one by merging their emissions.combineLatest()
: Combines the latest values from multiple observables into an array or object.forkJoin()
: Combines the last values emitted by multiple observables and emits a single value when all observables complete.
- Filtering Operators:
filter()
: Emits only those values from an observable that satisfy a specified condition.distinctUntilChanged()
: Emits a value only if it is different from the previous value.debounceTime()
: Emits a value from the source observable only after a specified duration has passed without any other value being emitted.
- Transformation Operators:
map()
: Transforms each value emitted by an observable into a new value.pluck()
: Extracts the value of a specified property from each emitted object.scan()
: Applies an accumulator function to each value emitted by an observable and emits each intermediate result.
- Error Handling Operators:
catchError()
: Handles errors emitted by the source observable and returns a new observable or throws an error.retry()
: Resubscribes to the source observable a specified number of times if an error occurs.
- Utility Operators:
tap()
: Performs side effects for each emission on the source observable without modifying the emitted values.delay()
: Delays the emissions from the source observable by a specified duration.
Steps to create Angular Project
Step 1: Install Node.js and npm
We need to install Node.js and node package manager to run Angular.
Download node.js from https://nodejs.org/en/download
Step 2: Install Angular CLI
We need to install Angular command Line interface for the creation and management of our Angular projects.
To install Angular CLI, run the following command:
npm install -g @angular/cli
Step 3: Create a New Angular Project:
Now, since we have angular CLI and node.js installed on our system, now we create an Angular project
Type the following command to create a new project
ng new demo-app
Step 4: Go to the Project directory
Now, after creating the project we need to navigate down to the project directory. We can navigate into our project directory with the following command.
cd demo-app
Step 5: Run the application
Since, we have navigated to our project directory, we can run our application with the following command
ng serve
Folder Structure:
Folder Structure
Dependencies:
"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Common RxJS operators
Here's an overview of some common RxJS operators and how they can be used in Angular:
1. map
The map operator is used to convert the values emitted by an observable into new values.. This is often used to perform some kind of data transformation.
HTML
<!-- app.component.html -->
<div>
<h1>Output Data using map operator</h1>
<ul>
<li *ngFor="let data of outputData">{{ data }}</li>
</ul>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
outputData: number[] = [];
ngOnInit() {
const observable = of(1, 2, 3, 4, 5);
observable.pipe(
map(data => data * 2)
).subscribe(data => {
this.outputData.push(data);
});
}
}
Output:

2. filter
The filter operator is used to remove values from an observable depending on a criterion.
HTML
<!-- app.component.html -->
<div>
<h1>Output Data using filter operator</h1>
<ul>
<li *ngFor="let data of outputData">{{ data }}</li>
</ul>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable, of } from 'rxjs';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
outputData: number[] = [];
ngOnInit() {
const observable = of(1, 2, 3, 4, 5, 11, 13, 17, 19, 0, 5, 7);
observable.pipe(
filter(data => data > 7)
).subscribe(data => {
this.outputData.push(data);
});
}
}
Output:

3. mergeMap (flatMap)
The mergeMap operator is used to merge multiple observables into a single observable by applying a function to each emitted value and flattening the resulting observables.
HTML
<!-- app.component.html -->
<div>
<h1>Output Data using the mergeMap operator</h1>
<ul>
<li *ngFor="let data of outputData | async">{{ data }}</li>
</ul>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable, of } from 'rxjs';
import { mergeMap, toArray } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
outputData: Observable<number[]>;
ngOnInit() {
const observable = of(1, 2, 3, 4, 5);
const square = (data: number) => of(data * data);
this.outputData = observable.pipe(
mergeMap((data) => {
return square(data);
}),
toArray()
);
}
}
Output:

4. combineLatest
The combineLatest operator combines the latest values from multiple observables whenever any of the observables emit a new value.
HTML
<!-- app.component.html -->
<div>
<h2>combineLatest Example</h2>
<p>Latest values from observables:</p>
<ul>
<li *ngFor="let pair of combinedValues">
{{ pair[0] }} - {{ pair[1] }}
</li>
</ul>
</div>
JavaScript
// app.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable, combineLatest, of } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
combinedValues: [number, string][] = [];
ngOnInit() {
const observable1: Observable<number> = of(1, 2, 3, 4);
const observable2: Observable<string> = of('A', 'B', 'C', 'D');
const combinedObservable: Observable<[number, string]> = combineLatest(
observable1,
observable2
);
combinedObservable.subscribe((pair) => {
this.combinedValues.push(pair);
});
}
}
Output:

Similar Reads
What is Interpolation in AngularJS ?
In this article, we will know about Interpolation in AngularJS, along with understanding the basic implementation. In AngularJS, Interpolation is a way to transfer the data from a TypeScript code to an HTML template (view), i.e. it is a method by which we can put an expression in between some text a
2 min read
Angular Versions
Angular has been an essential framework in web development, enabling developers to build dynamic and responsive web applications. Since its launch, Angular has evolved, introducing new features, performance improvements, and enhanced capabilities. Understanding the different versions of Angular and
4 min read
Paging in AngularJS
Pagination in AngularJS is a technique of dividing the contents of a particular large page into small pages and showing them in an indexed format in order. This technique is very popular in designing search engines in which, the most relatable content is presented on a priority basis. Also, the pagi
5 min read
AngularJS Animations
To create animation effects in AngularJS using the ngAnimate module, which provides support for CSS-based animations. Animation is something that is used to give a dynamic motion effect. Here HTML is transformed to give an illusion of motion using the ngAnimate module that gives us a combined effect
1 min read
AngularJS ng-options Directive
The ng-options Directive in AngularJS is used to build and bind HTML elements with options to a model property. It is used to specify <options> in a <select> list. It is designed specifically to populate the items on a dropdown list. This directive implements an array, in order to fill t
2 min read
How to use jQuery in Angular ?
In this tutorial, we will learn how we can use jQuery with Angular. There are two ways in which we can use jQuery with Angular as discussed below: Table of Content By installing jQuery using the npm commandUsing jQuery CDN to use itBy installing jQuery using the npm commandYou can install the jQuery
2 min read
Angular 4 | Introduction
Angular 4 was released 5 years after the official release of AngularJS. Between these two versions, Angular 2 was introduced which was a complete re-write of AngularJS. The 'MVC' architecture of AngularJS was discarded a new 'service-controller' architecture was introduced in Angular 2. After Angula
2 min read
AngularJS $location Service
The $location in AngularJS basically uses a window.location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS. There are various methods in the $location s
4 min read
Angular 7 | Introduction
Angular 7 is a TypeScript based front-end web framework by Google. It enables you to create Single Page Applications(SPA) with the help of the concept of components. The components in Angular 7 are structured like a tree i.e. there are parent and child components in which each child component is con
2 min read
What is scope in AngularJS ?
In this article, we will see what the scope is in AngularJS and how to use Scope, along with understanding its implementation through the examples. The Scope in AngularJS is the binding part between HTML (view) and JavaScript (controller) and it is a built-in object. It contains application data and
4 min read