基于其他复选框值反应形式的角度禁用复选框

问题描述 投票:0回答:0

我正在尝试根据其他复选框值禁用该复选框。 只能启用其中一个。

启用其中之一时出现此错误,因为已订阅:值大量涌入。

    at SafeSubscriber.__tryOrUnsub (Subscriber.js:191:32)
    at SafeSubscriber.next (Subscriber.js:122:22)
    at Subscriber._next (Subscriber.js:72:26)
    at Subscriber.next (Subscriber.js:49:18)
    at EventEmitter_.next (Subject.js:39:25)
    at EventEmitter_.emit (core.mjs:22784:15)
    at FormControl.disable (forms.mjs:2164:31)
    at Object.next (saxo-browser.component.ts:95:52)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183:16)
    at SafeSubscriber.next (Subscriber.js:122:22)

我不知道该怎么做。 这是我的代码,

成分:

  private initForm(): void {
    this.settingsForm = this.formBuilder.group({
      firstValue:[false],
      secondValue: [false]
    });

    this.settingsForm.get('firstValue').valueChanges.subscribe((value) => {
      if(value === true) {
        this.settingsForm.get('secondValue').disable()
      } else {
        this.settingsForm.get('secondValue').enable();
      }
    });
    
    this.settingsForm.get('secondValue').valueChanges.subscribe((value) => {
      if(value === true) {
        this.settingsForm.get('firstValue').disable()
      } else {
        this.settingsForm.get('firstValue').enable();
      }
    });
  }

html:

    <form name="settingsForm" novalidate [formGroup]="settingsForm">
        <label class="switch">
            <input formControlName="firstValue" type="checkbox" class="form-control">
            <span class="slider round"></span>
        </label>
        <label class="switch">
            <input formControlName="secondValue" type="checkbox" class="form-control">
            <span class="slider round"></span>
        </label>
    </form

angular typescript angular-reactive-forms
© www.soinside.com 2019 - 2024. All rights reserved.