Skip to content

Instantly share code, notes, and snippets.

@techiediaries
Last active February 12, 2020 03:55
Show Gist options
  • Save techiediaries/d48cf8e286c6294cd2ad9f46fbbc48f0 to your computer and use it in GitHub Desktop.
Save techiediaries/d48cf8e286c6294cd2ad9f46fbbc48f0 to your computer and use it in GitHub Desktop.
Angular 9 fromEvent example
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
@Component({
selector: 'app',
template: `
<p>
App works!
<button #btn> Click </button>
</p>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
@ViewChild('el') button: ElementRef;
ngAfterViewInit() {
Observable.fromEvent(this.button.nativeElement, 'click')
.subscribe(res => console.log(res));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment