matt
matt

Reputation: 749

fromEvent not getting keyboard events

Why isn't this getting the keyboard type events? Its not printing anything to console. I'm copying code from this page

  ngOnInit() {

    const searchElement = document.getElementById('search-input');

    const typeahead = fromEvent(searchElement, 'input').pipe(
      map((e: any) => e.target.value),
      tap(term => console.log('searching... ', term)),
      filter(text => text.length > 2),
      debounceTime(10),
      distinctUntilChanged(),
      switchMap(term => this.searchService.search(term))
    );

  }

HTML

<input type="text" id="search-input">

Upvotes: 2

Views: 98

Answers (2)

Rohit.007
Rohit.007

Reputation: 3502

You have not subscribed the observable, you can get the it emitted only after adding the below code

typeahead.subscribe(item => {
  console.log('subscribe');
});

check the working link

Upvotes: 1

Amit Chigadani
Amit Chigadani

Reputation: 29795

You are not subscribing to it, that is the reason nothing is emitted. Observables are not emitted if not subscribed

Add the following code :

typeahead.subscribe(data => {
 // Handle the data from the API
});

Upvotes: 2

Related Questions