The problem is the lifecycle hook you're using. The element is not yet creating in DOM when ngOnInit
is called. Instead, you should use ngAfterViewInit
.
Could you try the following code:
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { Observable, fromEvent } from 'rxjs';
@Component({
template: '<button #input>Button</button>'
})
export class ActionOverviewDescription implements AfterViewInit {
@ViewChild('input') button: ElementRef;
ngAfterViewInit() {
let buttonStream$ = Observable.fromEvent(this.button.nativeElement, 'click')
.subscribe(res => console.log(res));
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…