首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >debounceTime不使用搜索吗?Angular 9+

debounceTime不使用搜索吗?Angular 9+
EN

Stack Overflow用户
提问于 2021-11-22 17:10:53
回答 1查看 19关注 0票数 0

我试着在我的搜索中设置去抖动时间,但没有工作...检查我的代码:

代码语言:javascript
复制
  ngOnInit(): void {
      this.search.valueChanges.subscribe(
      (selectedValue) => {
        this.searchVal(selectedValue)
      }
    );
  }

  private getAllDrivers(query: string) {
    console.log('gar')
    return this.accountsService.getDrivers(query)
      .pipe(
        debounceTime(800),
        distinctUntilChanged(),
        map((data: any) => {
          data.body?.forEach((driver: IDriver) => {
            driver.name = `${driver.firstName} ${driver.lastName}`
          });
          return data.body || [];
        }),
        tap(() => noop, err => {
          this.errorMessage = err && err.message || 'Something goes wrong';
        })
      )
  }


public search: FormControl = new FormControl(null);

html:

代码语言:javascript
复制
 <input [formControl]="search">

为什么不使用这个代码呢?

当我每时每刻都在寻找某样东西的时候,我得到了http get ...I需要半秒,几秒的延迟……

我也在尝试:

代码语言:javascript
复制
  this.search.valueChanges.
   .pipe(debounceTime(800))
    subscribe(
     (selectedValue) => {
    this.searchVal(selectedValue)
  }
);

也没有工作..。

EN

回答 1

Stack Overflow用户

发布于 2021-11-22 17:16:39

您必须使用debounceTimeswitchMap,但您的第二个示例或多或少也应该有效。

假设this.search.valueChanges是可观察到的用户输入

代码语言:javascript
复制
this.search.valueChanges.pipe(
   debounceTime(800),
   distinctUntilChanged(),
  switchMap(query=>this.accountsService.getDrivers(query))
).subscibe(results => do sometinh)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70069770

复制
相关文章

相似问题

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