我需要帮助理解如何将这个函数转换为一个流以获得响应性和最新的结果。

huangapple go评论84阅读模式
英文:

I need help understanding how to turn this function into a stream for responsive and up-to-date results

问题

我在这个项目中实现了NGB-Typeahead,但我发现适用于它的过滤函数是在事后返回结果的。我需要提供当前的结果,我想使用流订阅应该可以解决,但是使用[ngbTypeahead]绑定让我感到困惑。

这是在Angular 7中的情况。

示例:

用户输入:000
结果:没有,因为我让它等待文本长度为3,当输入第三个字符时结果还不存在。

用户输入:之前的 + 0
结果:00000234,12000034,请注意只有3个被突出显示,因为这是之前的结果。现在已经请求了另一个结果的变化,但它要等到下一个字符被输入才会显示出来。

<input type="text" class="search" [(ngModel)]="typeAheadModel" [ngbTypeahead]="filter" [resultTemplate]="result" [inputFormatter]="NumFormatter" />
filter = (text$: Observable<string>) =>
    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(
        (term) =>
          term.length < 3
            ? []
            : (this.pageController.fetchPageSearchList(
                this._searchCriteriaSelection,
                term,
                '30'
              ),
              this._searchResults).slice(0, 5)
      )
    );

我很难理解如何将这个问题分解成一个可以订阅的操作和流,以使结果保持最新,而不使用async-await方法。

提前感谢任何帮助!

英文:

So I implemented NGB-Typeahead in this project but the filtering function I found that works for it returns a result after the fact. I need to provide results that are current and I figure using a stream subscription should work but using the [ngbTypeahead] binding is confusing me.

This is in Angular 7.

Example:

User inputs: 000
Results: nothing because I have it wait for length 3 in the text and the results don't exist when the 3rd
character is typed

User inputs: previous + 0
Results: 00000234 , 12000034 Note that only 3 are highlighted because this is the previous result. Now another result has been requested by the change but it won't show until the next character has been typed in.

&lt;input type=&quot;text&quot; class=&quot;search&quot; [(ngModel)]=&quot;typeAheadModel&quot; [ngbTypeahead]=&quot;filter&quot; [resultTemplate]=&quot;result&quot; [inputFormatter]=&quot;NumFormatter&quot; /&gt;
filter = (text$: Observable&lt;string&gt;) =&gt;
    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(
        (term) =&gt;
          term.length &lt; 3
            ? []
            : (this.pageController.fetchPageSearchList(
                this._searchCriteriaSelection,
                term,
                &#39;30&#39;
              ),
              this._searchResults).slice(0, 5)
      )
    ); 

I am having a hard time wrapping my head around how to break this down into an action and a stream that can be subscribed to that will keep the results current without using the async-await methodology.

Thanks in advance for any help!

答案1

得分: 1

尝试filter掉传递给事件流的事件。

演示

filter = (text$: Observable<string>) =>
  text$.pipe(
    debounceTime(200),
    distinctUntilChanged(),
    filter(term => term.length > 3),
    switchMap(term => this.pageController.fetchPageSearchList(term))
  );
英文:

Try to filter out events getting passed down the event stream.

DEMO
<!-- language: lang-js -->

filter = (text$: Observable &lt; string &gt; ) =&gt;
  text$.pipe(
    debounceTime(200),
    distinctUntilChanged(),
    filter(term =&gt; term.length &gt; 3),
    switchMap(term =&gt; this.pageController.fetchPageSearchList(term));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:11:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定