【问题标题】:Rxjs - DistinctUntilChanged() with keyup eventRxjs - DistinctUntilChanged() 与 keyup 事件
【发布时间】:2018-06-07 03:14:04
【问题描述】:

我正在使用 Rxjs 6,它根据表单字段中的 (keyup) 事件过滤 Firebase 返回的 observable。

当用户在表单字段中没有值的情况下一直按退格键时,我遇到了一个问题,然后看起来 observable 似乎在不断刷新。

使用 DistinctUntilChanged() 添加管道似乎没有效果:

Typescript 过滤功能:

updateFilter2() {
    const val = this.filteredValue.toLowerCase().toString().trim();

    if (this.filteredValue) {
        this.loadingIndicator = true;
        this.rows = this.svc.getData()
            .pipe(
                distinctUntilChanged(),
                debounceTime(300),
                map(_co => _co.filter(_company =>
                    _company['company'].toLowerCase().trim().indexOf(val) !== -1 || !val
                    ||
                    _company['name'].toLowerCase().trim().indexOf(val) !== -1 || !val
                    ||
                    _company['gender'].toLowerCase().trim().indexOf(val) !== -1 || !val
                )),
                tap(res => {
                    this.loadingIndicator = false;
                })
            );
    }
    else {
        this.rows = this.svc.getData()
            .pipe(distinctUntilChanged())
        this.loadingIndicator = false;
    }

    this.table.offset = 0;
}

HTML 模板:

<mat-form-field style="padding:8px;">
    <input
            type='text'
            matInput
            [(ngModel)] = "filteredValue"
            style='padding:8px;margin:15px auto;width:30%;'
            placeholder='Type to filter the name column...'
            (input)='updateFilter2()'
    />
</mat-form-field>

我有一个 Stackblitz 重现该行为:https://stackblitz.com/edit/angular-nyqcuk

还有其他方法可以解决吗?

谢谢

【问题讨论】:

  • 听(输入)事件而不是(键)似乎可以解决问题
  • 请不要链接到外部网站以提供您的代码除非您已经直接在您的答案中包含了代码的副本。如果其他网站出现故障、消失或更改其链接结构,则该问题无效。

标签: angular rxjs


【解决方案1】:

问题是总是生成getData。您首先查看更改,然后切换映射以获取数据。所以你必须有一个可观察的变化。使用“FormControl”,而不是带有 [(ngModel)] 的输入 所以,在你的 .html

<input type="text" [formControl]="search">

代码必须是

  search= new FormControl();       //Declare the formControl

  constructor() {}

  ngOnInit() {
    this.search.valueChanges.pipe(
         debounceTime(400),
         distinctUntilChanged(),
         tap((term)=>{
              //here the value has changed
              this.loadingIndicator = true;
         }),
         switchMap(filteredValue=> {
              //We not return the value changed, 
              return this.svc.getData().pipe(
                     map(_co => {
                         return _co.filter(...)
                     }),
                     tap(()=>{
                         this.loadingIndicator=false;
                     }))
          })).subscribe(result=>{
             this.result=result
          })
  }

【讨论】:

  • 感谢@Eliseo 的投入
  • 为了补全,必须在模块中导入FormsModule、ReactiveFormsModule。 stackoverflow.com/questions/40262535/…
  • distinctUntilChanged() 在这里做什么?我似乎看不出有没有它的区别。
  • @DeborahK,这只是一个“检查”,例如您键入并更正(例如键入 a 并删除),按 Ctrl+V 或类似
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2017-10-08
相关资源
最近更新 更多