From event in RXJS

From event in RXJS

In this tutorial, we will learn about from event of RXJS.

Turn the event into an observable sequence

This operator will give output as an observable that is to be used on elements that emit events for example buttons, clicks, etc.

Parameters

target − The target is the dom element

eventName − eventName you want to capture for example click, mouseover, etc.

Return value

It returns an observable.

syntax:

fromEvent(target: eventtarget, eventName: string): Observable

Example:

<button #addBtn class="btn btn-primary">Add</button>

---------------------------
@ViewChild("addBtn", { static: false }) addBtn: ElementRef;

let count = 1;
fromEvent(this.addBtn.nativeElement, "click").subscribe((response) => {
  let countVal = count++;     
  console.log(countVal);
});

Output:
1,2,3,4,5.....

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

You might also like

Leave a Comment