首页 > 解决方案 > RXJS Stream 在每个复选框/单选更改时触发,但预期行为仅在表单提交时触发

问题描述

我的 UI 上有两个选项卡。

第一个选项卡(默认) - 有一个输入文本框,当用户提交它时,会向服务器发送一个 http 调用(按预期工作)

第二个选项卡 - 相同的输入文本框 + 复选框和单选按钮列表,其中至少必须选择一个。然后在提交表单时,发送 http 调用。

现在,问题是 - 当用户提交第二个选项卡的表单时,我正在汇总选定的复选框和单选值。我已经为 selectedCheckbox 和 selectedRadioButton 提供了一个单独的流,它们按预期工作。用户首次提交表单后会出现此问题。在每次复选框/收音机更改时,都会将 http 调用发送到服务器。我希望它仅在提交表单时才这样做(这也在发生)。所以现在 - http 调用正在表单提交(需要)以及单选/复选框更改(不需要)上发送

下面是代码片段。

我不确定我错过了什么。任何建议都受到高度赞赏。

detailsByAccount$: Observable<MyType> = this.selectedTab$.pipe(
switchMap((tab) => {
   //enteredAccount$ = account submitted through form
  return this.enteredAccount$.pipe(
    switchMap((account) => {

      // Server Http call to get the response
      if (tab === 'X') {
        return this.myService.getDetails(account); // This section works fine
      }

      // After the form is submitted once, this stream is triggered every time, 
     //checkbox is checked or radio button is clicked, probably because both the values are available

        return combineLatest(this.selectedCheckbox$, this.selectedRadio$).pipe(
            concatMap(([selectedCheckbox, selectedRadio]) => this.myservice.getDetails(account, selectedCheckbox, selectedRadio)
        )
      );
      })
      );
    })
  )

标签: angularrxjsobservable

解决方案


推荐阅读