How to Create Observable data stream using fromEvent method in AngularJS ?
Last Updated :
29 Jul, 2024
In this article, we will learn how Observable supports data sharing between publishers and subscribers in an angular application. An Observable is referred to as a better technique for event handling, asynchronous programming, and handling multiple values as compared to techniques like promises.
- Observable: Represents a stream or source of data that can arrive over time. But the observable is meaningless without a Subscriber.
- Subscriber: Simply, it's a piece of code that keeps an eye on the Observable and its data. It fetches the data and makes it usable for the component.
Prerequisite Knowledge:
- Angular v6.2.9 or above and RxJs v6.4.0 or above
- Node v10.15.3 or above and NPM v6.4.1 or above
(Note: RxJs is automatically installed with Angular, similarly NPM is automatically installed with Node)
You must have understood the basic concept of Observable and Subscribe in Angular. If not, please follow the article How to implement Observable-Subscribe in Angular application.
Project Structure: It will look like this

Most common and easiest way to create Observable: The most common and easiest way to create Observable is using methods provided by the RxJs library. It’s a 3rd party library that gets automatically installed when you create a new angular project. One such method is fromEvent. We will discuss other methods like Interval and Timer later.
FromEvent: FromEvent is a method provided by RxJs to create Observable. The best thing is that we can create Observable from DOM events directly. By DOM events, it means click event, key up events, scroll events, etc. that is a simple mouse click, we can create a stream of data, i.e. an Observable. FromEvent is going to help us with that.
Syntax:
fromEvent(target,eventName)
.subscribe((res)=>{console.log(res)})
Let’s talk about the two parameters.
target: It can be any DOM EventTarget such as a button, radio button, slider, etc. Whatever DOM EventTarget we are using, it should have methods to register/unregister the event handler. The elements we discussed above are in-built mechanisms. We don’t have to write code for that.
eventName: It is the type of event we want to listen to.
Example: Click, mousedown, mouseup, click, dblclick, mousemove, mouseover, mousewheel, mouseout, touchstart, touchmove, touchend, touchcancel, keydown, keypress, keyup, etc.
Approach: We will create a simple button “Add Book” on the UI and with every single click on this button, we will create one data stream. To keep it simple, our data stream will contain only one hard-coded string, i.e. “Book”. With every click, we will keep on generating Book 1, Book 2, Book 3, and so on. To display this stream on the UI, we will create one empty <ul> and keep on adding each Book as one list item, i.e. <li> into <ul> in run time.
Preparation: To create an observable from any event fired by a DOM element, first, we need to get a reference to that DOM Element. This can be done using ViewChild and ElementRef. So, the code for this can be written as:
- HTML:
<button #addBtn>Add</button>
- TypeScript:
@ViewChild(‘addBtn’, {static: true}) addBtn: ElementRef;
Here this.addBtn.nativeElement returns the native DOM element. This is going to be our target (or EventTarget to be precise), which is the first argument for the fromEvent method. The second argument is ‘click’ because we are using a button click. So, the code becomes:JavaScript:fromEvent(this.addBtn.nativeElement, 'click')
.subscribe((res)=>{console.log(res)}
One small question arises! Where do we put this line of code?
Well, the best place is ngAfterViewInit. This is because ViewChild will not initialize the addBtn element until and unless ngOnInit gets completed. Hence, we will put this code inside ngAfterViewInit because priority wise ngOnInit is the first method to be executed when the component loads.
JavaScript:
ngAfterViewInit() {
fromEvent(this.addBtn.nativeElement, 'click')
.subscribe((res) => {
console.log(res);
})
}
If we click on the button, we'll get this on the console:
All these are data steams which are generated when we clicked the button.Let's get started by converting it into a working code.
- Step 1: Create a new Angular project
ng new observable-app
- Step 2: Create one component called FromEventComponent
ng g c from-event
4 new files should be created and app.module.ts should get updated;
All required files and changes - Step 3: Create a service PrintService
ng g s print-service
We can do this without service but to make our code more dynamic and meaningful. Let's use a service. This is being done. Now let's make the necessary changes to make it work.
from-event.component.html
<!-- To display the data/stream on
UI we will keep on adding each stream
as a list item in ul below -->
<ul id="elementContainer"></ul>
<!-- Button which will create a stream -->
<button #addBtn>Add Book</button>
print-service.service.ts
printStream(containerName, stream) {
let element = document.createElement('li');
element.innerText = stream;
document.getElementById(containerName).appendChild(element);
}
As we have created a method called printStream. It will accept two arguments. The first argument is the DOM element with reference to which data will be appended and the second argument is the data or stream that I want to append. Now we can inject this service into the component and use this method:
from-event.component.ts
ngAfterViewInit() {
let count = 1;
fromEvent(this.addBtn.nativeElement, "click")
.subscribe((res) => {
this._printService.printStream(
"elementContainer", "Book " + count++);
})
}
One count variable we've taken to distinguish between streams. It will keep on adding auto-incremented integer with "Book". Here, at the line "this._printService.printStream('elementContainer', 'Book ' + count++)", we are telling where to be appended and what to be appended.
Final Code:
from-event.component.html
<!-- To display the data/stream
on UI we will keep on adding
each stream as a list item in
ul below -->
<ul id="elementContainer"></ul>
<!--Button which will create a stream-->
<button #addBtn>Add Book</button>
from-event.component.ts
import { Component, ElementRef, OnInit, ViewChild } from "@angular/core";
import { fromEvent } from "rxjs";
import { PrintServiceService } from "../print-service.service";
@Component({
selector: "app-from-event",
templateUrl: "./from-event.component.html",
styleUrls: ["./from-event.component.css"],
})
export class FromEventComponent implements OnInit {
@ViewChild("addBtn", { static: true }) addBtn: ElementRef;
constructor(private _printService: PrintServiceService) {}
ngOnInit() {}
ngAfterViewInit() {
let count = 1;
fromEvent(this.addBtn.nativeElement, "click").subscribe((res) => {
this._printService.printStream("elementContainer", "Book " + count++);
});
}
}
print-service.service.ts
import { Injectable } from "@angular/core";
@Injectable({
providedIn: "root",
})
export class PrintServiceService {
constructor() {}
printStream(containerName, stream) {
// Creating li element in run time
let element = document.createElement("li");
element.innerText = stream;
// Appending li to ul
document.getElementById(containerName).appendChild(element);
}
}
app.component.html
...
<app-from-event></app-from-event>
Output: You can see streams being treated with every mouse click. In upcoming articles, we will learn about Interval and Timer. Which is nothing but another way of creating Observable with added facilities. Hope this helps.
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network
A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read