Created
December 4, 2018 06:59
-
-
Save LayZeeDK/377c77ee0b95807956a27ebf3d4cb856 to your computer and use it in GitHub Desktop.
Hero search: Container component model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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