我使用的是Angular7,Typeahead 4。我有一个带有NgBootstrap指令的输入域。
当我遵循文档中编写的自定义搜索函数格式时,工作正常。当我尝试修改格式时,我得到一个错误:
我得到了这个错误:
core.js:12584 ERROR TypeError: Cannot set property 'searching' of undefined我做错了什么?
这是有效的:
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this.searchService.fetchData(term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap(() => this.searching = false)
);这不起作用:
search = function (text$: Observable<string>) {
return text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this.searchService.fetchData(term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap(() => this.searching = false)
);
}发布于 2018-11-09 20:54:31
感谢JB Nizet,他给我指明了正确的方向。这是工作代码(参见第一行和最后一行中的花括号)
search = (text$: Observable<string>) => {
// do some logic before text$.pipe call
console.log(text$);
text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this.searchService.fetchData(term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap(() => this.searching = false)
);
}https://stackoverflow.com/questions/53225112
复制相似问题