Skip to content

Instantly share code, notes, and snippets.

@LayZeeDK
Created December 4, 2018 06:59
Show Gist options
  • Save LayZeeDK/377c77ee0b95807956a27ebf3d4cb856 to your computer and use it in GitHub Desktop.
Save LayZeeDK/377c77ee0b95807956a27ebf3d4cb856 to your computer and use it in GitHub Desktop.
Hero search: Container component model
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-hero-search',
templateUrl: './hero-search.container.html',
})
export class HeroSearchContainerComponent {
private searchTerms: Subject<string> = new Subject();
heroes$: Observable<Hero[]> = this.searchTerms.pipe(
// switch to new search observable each time the term changes
switchMap(term => this.heroService.searchHeroes(term)),
);
constructor(private heroService: HeroService) {}
// Push a search term into the observable stream.
search(term: string): void {
this.searchTerms.next(term);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment