【问题标题】:how to use lodash _.debounce in angular如何在角度中使用 lodash _.debounce
【发布时间】:2020-10-18 13:33:46
【问题描述】:

我尝试在我的 Angular 应用程序的 lodash 库中使用函数 _.debounce。

我的代码:

   onKeyup(event) {
    if (event.length >= 3) {
      this.getCities(event);
    }
  }

  public getCities(namePrefix: any) {

    this.citiesSrv.getCities(namePrefix).toPromise().catch(err => console.log(err)).then(results => {
      this.options = results["data"].map(x => x.name + ", " + x.country);
    }
    ).then(test => {
      this.filteredOptions = this.myControl.valueChanges
        .pipe(
          startWith(''),
          map(value => this._filter(value))
        );
    })
  }

我想在 OnKeyup 函数中延迟调用 getCities。 什么是正确的方法?

谢谢

【问题讨论】:

    标签: angular lodash debounce


    【解决方案1】:

    您需要将debounce 包裹在getCities 周围并调用它。所以在你的组件文件里面 在下面的代码 sn-p 中,我将其反跳到 400 毫秒

    debouncedGetCitities = _.debounce(params => this.getCities(params), 400);
    ...
    onKeyup(event) {
        if (event.length >= 3) {
          this.debouncedGetCitities(event);
        }
      }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2019-04-08
      • 2018-05-28
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多