如何清空一个 Angular Observable 数组?
How to Empty an Angular Observable Array?
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[]>([]);
}
我有一个带有搜索建议的搜索框有可观察数组。在 onClear 事件期间,可观察数组应该为空。
你可以试试
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);
}),
);
(所以 this.heroes$
会发出 []
如果 this.searchTerms
发出 ''
)
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[]>([]);
}
我有一个带有搜索建议的搜索框有可观察数组。在 onClear 事件期间,可观察数组应该为空。
你可以试试
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);
}),
);
(所以 this.heroes$
会发出 []
如果 this.searchTerms
发出 ''
)