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
How to change the date format using AngularJS ?
In this article, we will see how to change the date format using AngularJS. AngularJS provides 2 different ways to change the format of the date. It can be achieved by the following approaches: Using HTML Template BindingUsing JavaScript ControllerHTML Template Binding: In this method, we use the pi
3 min read
How to Retrieve Data using HTTP with Observables in Angular ?
Most applications obtain data from the backend server. They need to make an HTTP GET request. In this article, we'll look at making an HTTP request and map the result/response in a local array. This array can be used to display or filter the items as we want. The most important thing here is using O
4 min read
How to create the dynamic forms in AngularJS ?
In AngularJS, we need to create dynamic forms to make the user interact. We can create and apply different behaviors to the forms to represent and accept the data from the user. We can create dynamic forms using ng-repeat and ng-show in AngularJS. In this article, we will see how we can create dynam
6 min read
How to fetch the details using ng-repeat in AngularJS ?
In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angul
2 min read
How to create a To-Do list using Drag and Drop in Angular 7 ?
We can easily create a To-Do list using Drag-Drop module provided by angular Component Development Kit (CDK). First of all, create an angular component by using the following command- ng g c To-Do Now import CdkDragDrop, moveItemInArray, transferArrayItem from @angular/cdk/drag-drop to our to-Do com
2 min read
How to get file content and other details in AngularJS?
We can get the file content by using some basic angular functions and other details like the name and size of the file in AngularJS. To understand it look into the below example where both HTML and JS files are implemented. Note: Consider below two files are of same component in angular. app.module.
2 min read
How to get original element from ng-click in AngularJS ?
In AngularJS, we can get the original DOM element that fired the ng-click event using the event's currentTarget property. Getting the event's currentTarget can be useful when we want to track an element's activity, in this case, whether and how many times a particular element is clicked, or we want
3 min read
How to Create a new module in Angular ?
Modules are most important when it comes to building strong and scalable Angular applications. They help organize your code, promote modularity, and improve maintainability. It encourages collaboration among developers by grouping related components, directives, pipes, and services. In this article,
3 min read
How to delete an item or object from the array using ng-click ?
The task is to delete the item from the list when the button is clicked. This all should be done by using ng-click. This is done by using the splice() method. The syntax for the method is given below. Syntax for splice() function: array.splice(indexno, noofitems(n), item-1, item-2, ..., item-n) Exam
2 min read
Angular 7 | Angular Data Services using Observable
Observables Observable manage async data and a few other useful patterns. Observables are similar to Promises but with a few key differences. Unlike Promises, Observables emit multiple values over time. In real scenarios, web socket or real-time based data or event handlers can emit multiple values
4 min read