【发布时间】:2018-10-03 12:50:26
【问题描述】:
我正在尝试使用可过滤的表格。 我有这样的 ListComponent:
export class CompanyListComponent implements OnInit {
companies$: Observable<Company[]>;
private searchTerms = new Subject<string>();
constructor(private companyService: CompanyService) { }
ngOnInit() {
this.companies$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(300),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => this.companyService.searchCompanies(term)),
);
}
search(term: string): void {
this.searchTerms.next(term);
}
我的 html 看起来像这样:
<input #searchBox type="text" class="form-control" id="search-box" (keyup)="search(searchBox.value)">
<table class="table">
<tr *ngFor="let company of companies$ | async">
<td>{{company.name}}</td>
</tr>
</table>
当我在搜索输入中写入时,表格会按应有的方式过滤。但是当我刷新页面时,表中根本没有数据。它出现在我在输入中输入内容之后。加载页面时我应该怎么做才能获取所有表格数据?我试图在 ngOnInit 中开始搜索,但这些尝试无济于事。
【问题讨论】: