Angular 2 - subscribing to Observable.fromEvent error: “Invalid event target”

前端 未结 2 1015
无人共我
无人共我 2020-12-14 17:42

I am getting a weird error when trying to subscribe to an Observable.

Here is a watered down version of the code which presents the problem:

2条回答
  •  一整个雨季
    2020-12-14 18:15

    If you want to access it in ngOnInit event then you would have to use { static: true } property of ViewChild something like this:

    import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
    import { Observable, fromEvent } from 'rxjs';
    
    @Component({
      template: ''
    })
    export class ActionOverviewDescription implements OnInit {
      @ViewChild('input', { static: true }) button: ElementRef;
    
      ngOnInit() {
        let buttonStream$ = Observable.fromEvent(this.button.nativeElement, 'click')
            .subscribe(res => console.log(res));
    
      }
    }
    

提交回复
热议问题