Skip to content

Instantly share code, notes, and snippets.

@nicowernli
Created August 31, 2018 10:16
Show Gist options
  • Save nicowernli/e19bbf0d6fe098d854fc84244f676841 to your computer and use it in GitHub Desktop.
Save nicowernli/e19bbf0d6fe098d854fc84244f676841 to your computer and use it in GitHub Desktop.
Debounce time included
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap, debounceTime } from 'rxjs/operators';
import { PlanetService } from '../planet.service';
import { Planet } from '../planet';
@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html'
})
export class SearchBarComponent implements OnInit {
planets$: Observable<Planet[]>;
search = new FormControl();
constructor(private service: PlanetService) {}
ngOnInit() {
this.planets$ = this.search.valueChanges.pipe(
debounceTime(250),
switchMap<string, Planet[]>(value => this.service.search(value))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment