我目前正在做的是設置輸入雙向綁定到appRequestParams.appName
,並在每個keyup事件中,將調用fetchApps()
方法。防跳輸入到http服務調用
我正在嘗試消除輸入,以便它不會立即在每個keyup上觸發後端http請求。
我已經閱讀了關於如何做這件事的方法,以及我在這裏所得到的結果。
HTML
<input #searchBox id="search" mdInput placeholder="Search by list of apps" (keyup)="search(searchBox.value)" />
打字稿
private searchTerms = new Subject<string>();
search(value: string): void {
this.searchTerms.next(value);
}
fetchApps() {
this.appService.query({
appName: this.appRequestParams.appName ? this.appRequestParams.appName : null,
}).subscribe(
(res: ResponseWrapper) => this.onSuccess(res.json, res.headers),
(res: ResponseWrapper) => this.onError(res.json)
);
}
ngOnInit() {
this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.switchMap((value: string) => {
this.appRequestParams.appName = value;
this.fetchApps();
});
}
錯誤對.switchMap()
行:你的代碼
Argument of type '(value: string) => void' is not assignable to parameter of type '(value: string, index: number) => ObservableInput<{}>'.
Type 'void' is not assignable to type 'ObservableInput<{}>'.
問題出在返回類型。請檢查您的服務的退貨類型 – Gautam