Angular7 NgBootstrap提前搜索

时间:2018-11-09 11:42:51

标签: angular

我正在使用Angular7,NgBootstrap4。我有一个带有Typeahead指令的输入字段。

当我遵循文档中编写的自定义搜索功能格式时,效果很好。尝试修改格式时,出现错误:

我收到此错误:

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)

    );

  }

1 个答案:

答案 0 :(得分:0)

感谢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)

    );

}