根据combinateLatest上的文档,我无法在启动时将其初始化为包含值。这导致我的组件为空。有什么办法可以解决,以便默认显示this.projects?
ngOnInit() {
this.filtered =
combineLatest(of(this.projects), this.searchService.observable)
.pipe(
map(([projects, searchString]: [{ name: string, preview: string, text: string }[], string]) =>
projects.filter(x => x.name.toLowerCase().includes(searchString.toLowerCase()))
));
this.filtered.subscribe();
}
答案 0 :(得分:2)
是的,请使用startWith
运算符。
ngOnInit() {
this.filtered =
combineLatest(of(this.projects), this.searchService.observable.pipe(startWith('')))
.pipe(
map(([projects, searchString]: [{ name: string, preview: string, text: string }[], string]) =>
projects.filter(x => x.name.toLowerCase().includes(searchString.toLowerCase()))
));
this.filtered.subscribe();
}