首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular7 NgBootstrap Typeahead搜索

Angular7 NgBootstrap Typeahead搜索
EN

Stack Overflow用户
提问于 2018-11-09 19:42:51
回答 1查看 342关注 0票数 1

我使用的是Angular7,Typeahead 4。我有一个带有NgBootstrap指令的输入域。

当我遵循文档中编写的自定义搜索函数格式时,工作正常。当我尝试修改格式时,我得到一个错误:

我得到了这个错误:

代码语言:javascript
复制
core.js:12584 ERROR TypeError: Cannot set property 'searching' of undefined

我做错了什么?

这是有效的:

代码语言:javascript
复制
 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)

    );

这不起作用:

代码语言:javascript
复制
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)

    );

  }
EN

回答 1

Stack Overflow用户

发布于 2018-11-09 20:54:31

感谢JB Nizet,他给我指明了正确的方向。这是工作代码(参见第一行和最后一行中的花括号)

代码语言:javascript
复制
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)

    );

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53225112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档