Skip to content

Instantly share code, notes, and snippets.

@javiergamarra
Created March 12, 2018 21:38
Show Gist options
  • Save javiergamarra/fbfb523ce3bcc3e651d853740d86752b to your computer and use it in GitHub Desktop.
Save javiergamarra/fbfb523ce3bcc3e651d853740d86752b to your computer and use it in GitHub Desktop.
Observable.fromEvent in angular5
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
@Component({
selector: 'app-hola',
template: `
<p>
hola works!
<button #hola>Hola!</button>
</p>
`,
styleUrls: ['./hola.component.css']
})
export class HolaComponent implements AfterViewInit {
@ViewChild('hola') 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