首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 8 Rxjs distinctUntilChanged操作符

Angular 8 Rxjs distinctUntilChanged操作符
EN

Stack Overflow用户
提问于 2020-10-30 23:43:36
回答 1查看 486关注 0票数 2

我的代码和distinctUntilChanged操作符有问题。

这是我的代码:

代码语言:javascript
复制
ngAfterViewInit() {
    fromEvent(this.headlineInput.nativeElement, 'blur').pipe(
      takeUntil(this.unsubscribe$),
      map((evt: any) => evt.target.value),
      distinctUntilChanged()
    ).subscribe((text: string) => {
      this.onInputValueChanges(text);
    });
  }

在这段代码中,我想在模糊上运行this.onInputValueChanges(text)方法,但只在标题输入发生变化时才运行。如果不是,则不应在subscribe中运行此方法。我认为我可以使用distinctUntilChanged,但它似乎不起作用。每当我的标题输入模糊时,它就会运行。我做错了什么吗?有人能给我指出这段代码的错误之处吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-10-31 00:27:10

如果你想变得懒惰,你可以只检查对象的值是否与之前的发射不同。

代码语言:javascript
复制
ngAfterViewInit() {
    fromEvent(this.headlineInput.nativeElement, 'blur').pipe(
      takeUntil(this.unsubscribe$),
      map((evt: any) => evt.target.value),
      // don't run if the stringified version of the object is the same.
      distinctUntilChanged((pre: any, curr: any) => JSON.stringify(pre) === JSON.stringify(curr)),
    ).subscribe((text: string) => {
      this.onInputValueChanges(text);
    });
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64611512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档