Reputation: 423
heroes$: Observable<Hero[]>;
// Push a search term into the observable stream.
search(term: string): void {
this.searchTerms.next(term);
}
ngOnInit(): void {
this.heroes$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(500),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => this.searchService.searchHeroes(term)),
);
}
onClear(event){
console.log('clear observable');
//clear array
// I have Tried these two things to clear observable array but it not..
this.heroes$.map(Hero => Hero = []);
Observable.of<Hero[]>([]);
}
I have a Searh box with search suggestion has observable Array.. during onClear event the the Observable Array should Empty.
Upvotes: 0
Views: 4378
Reputation: 10009
You could try something like
this.heroes$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(500),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => {
if (term === '') return Observable.of<Hero[]>([]);
else return this.searchService.searchHeroes(term);
}),
);
(so this.heroes$
would emit []
if this.searchTerms
emits ''
)
Upvotes: 1