角度 2 中的条件 distinctUntilChanged

alc*_*cha 0 rxjs angular

我有一个过滤器类,我可以在任何需要过滤器功能的地方继承它。

export class FilterUtil{
…
  public extFilterSubject = new Subject<any>();
  constructor(){
    this.extFilterSubject
      .pipe(map(event => event)
        , debounceTime(300)
        , distinctUntilChanged())
      .subscribe((searchKeyword) => {
         //filter logic here.
      })
   }
}
Run Code Online (Sandbox Code Playgroud)

这有效。

我想要完成的事情是有条件地应用 distinctUntilUnchanged 。我遇到过一种我不想要 distinctUntilUnchanged 运算符的情况。

我查看了 distinctUntilUnchanged官方文档。但我找不到任何相关的东西。

谢谢。

小智 5

它只能将当前值与最后发出的值进行比较。如果您想应用自己的条件。

distinctUntilChanged((valueOne: any, valueTwo: any) => {
            if (Your Condition) {
                return true; // This means values are equal, it will not emit the current value
            } else {
                return false;// This means the values are different, so it will emit
            }
        })
Run Code Online (Sandbox Code Playgroud)