【问题标题】:Angular 11 reactive form subscribe to valuechanges of input subscribing infinitelyAngular 11反应形式订阅输入订阅的值变化无限
【发布时间】:2021-12-10 19:15:53
【问题描述】:

我正在尝试订阅响应式表单输入控件的值更改。但这正在不断被订阅。我不确定我的代码有什么问题。谁能帮我解决这个问题?

this.sectionForm
            ?.get('sectionText')
            .valueChanges.pipe(debounceTime(1000))
            .subscribe((_newVal) => { //getting subscribed continously
                if (this.attributes?.sectionName != _newVal) {
                    this.attributes.sectionName = _newVal;
                }
                this._validatorFunctionsService.validateAllForms();
            });

【问题讨论】:

  • //getting subscribed continously,你的意思是该方法被多次调用?
  • 没有。它被多次订阅
  • 请提供包含上述代码的完整代码。也就是说,如果可能的话,调用上面这段代码的整个方法。

标签: rxjs angular-reactive-forms angular11


【解决方案1】:

distinctUntilChanged 与输入控件一起使用是一种很好的做法。

this.sectionForm?.get('sectionText').valueChanges.pipe(
    debounceTime(1000),
    distinctUntilChanged(),
)
.subscribe((_newVal) => {
   if (this.attributes?.sectionName != _newVal) {
      this.attributes.sectionName = _newVal;
   }
   this._validatorFunctionsService.validateAllForms();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2020-10-11
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    相关资源
    最近更新 更多