angular - 如何使用 typescript 修复 Angular 5 中的 ‘debounc

我有一个我检测到 keyup 的输入搜索元素,我想使用 debounce 来限制对 API 的请求,但我无法让它工作。我只是想测试 debounceTime 和 distinctUntilChanged。

我已经尝试过 (Keyup) 但无法正常工作。

<input (keyup)="onKeyUp($event)"  id="testInput" autocomplete="off" type="text" name="searchFilterText" class="m-list-search__form-input" value="" placeholder="Search...">

这是 typescript 文件中的代码。

searchInput = document.getElementById('testInput');
observable: any;

/// <summary>
/// Process the search term provided on key up for the input field.
/// </summary>
/// <param name="searchTerm">The search term.</param>
onKeyUp(event: KeyboardEvent) {
    //console.log(event);
    let element = event.target as HTMLInputElement;
    let value = element.value;

    this.observable = fromEvent(this.searchInput, event.type)
        .debounceTime(500) // millisecs until value gets emitted.
        .distinctUntilChanged()
        .subscribe(function (event) {
            console.log(event.target.value);
        });
}

预期结果是控制台日志中使用 debounceTime 和 distinctUntilChanged 的​​延迟搜索结果值。

最佳答案

你可以试试这个:

模板

<input  
       id="testInput" autocomplete="off"
       type="text" #searchText
       name="searchFilterText" class="m-list-search__form-input"
       value=""
       placeholder="Search...">

注意模板引用变量:#searchText。这允许在不需要 getElementById 的情况下访问输入元素(通常不建议在 Angular 应用程序中使用)。

组件

import { Component, AfterViewInit, ViewChild } from '@angular/core';
import { fromEvent } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Component({
   templateUrl: './search.component.html'
})
export class SearchComponent implements AfterViewInit {
  @ViewChild('searchText') searchTextRef;

  ngAfterViewInit() {
    if (this.searchTextRef) {
      fromEvent(this.searchTextRef.nativeElement, 'keyup')
        .pipe(
            debounceTime(500),
            distinctUntilChanged()
        ).subscribe(
          value => console.log(this.searchTextRef.nativeElement.value)
        )
    }
  }
}

此代码使用 @ViewChild 获取对用 #searchText 模板引用变量标记的元素的引用。

然后它使用类似于您为 debounceTime 使用的代码。

我这里有一个 stackblitz:https://stackblitz.com/edit/angular-debounce-deborahk

您可以在这里找到更多信息:Observable.fromEvent - RXJS

注意:如果您使用响应式(Reactive)表单,这会更容易,因为您可以直接访问表单上任何输入元素的 valueChanges observable。

响应式表单

模板

<input  
       id="testInput"
       [formControl]="search"
       autocomplete="off"
       type="text"
       class="m-list-search__form-input"
       value=""
       placeholder="Search...">

注意 formControl 指令。

组件

  // For reactive forms
  search = new FormControl();

  ngOnInit() {
    this.search.valueChanges
            .pipe(
          debounceTime(500),
          distinctUntilChanged()
        ).subscribe(
          value => console.log("Reactive Forms implementation: " + value)
        )
  }

关于angular - 如何使用 typescript 修复 Angular 5 中的 ‘debounceTime & distinctUntilChanged | RxJS’ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54757071/

相关文章:

scala - 使用 Scala 将多列转换为 Spark Dataframe 上的一列 map

amazon-web-services - 是否可以在控制台中编辑 AWS Lambda 层代码?

scala - 如何测量 Cats IO 效果中的耗时?

unicode - 为什么 unicode 代码点的 UTF-8 编码不能放入 3 个字节

google-photos-api - 如何使用 Google Photos API 的 baseU

r - R中大数据的轮廓计算

ag-grid - 使用 forEachNode 的行选择非常慢

reactjs - 覆盖 Material UI 扩展面板摘要

reactjs - 当 URL 包含多个路径时 React Router 不工作

xamarin - 使用 xamarin 表单从页面中删除向后滑动手势