Skip to content

Instantly share code, notes, and snippets.

@nicowernli
Created August 31, 2018 09:54
Show Gist options
  • Save nicowernli/f7d9d29b1dc49896fc1ee219f5d7ba71 to your computer and use it in GitHub Desktop.
Save nicowernli/f7d9d29b1dc49896fc1ee219f5d7ba71 to your computer and use it in GitHub Desktop.
The Search Bar Component base
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { switchMap } 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(
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