【发布时间】:2017-06-05 07:20:01
【问题描述】:
我目前正在做的是将输入 2-way-binding 设置为 appRequestParams.appName,并且在每个 keyup 事件上,都会调用 fetchApps() 方法。
我正在尝试对输入进行去抖动,这样它就不会立即在每个 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<{}>'.
【问题讨论】:
-
问题在于返回类型。请检查您的服务的返回类型
标签: html angular typescript