简体   繁体   English

如何订阅两个输入字段并使它们在 Angular 2 中相互切换?

[英]How can I subscribe to two input fields and make them toggle each other in Angular 2?

I have a checkbox and input field,我有一个复选框和输入字段,

Case One :案例一

  • If the input field isn't empty checkbox will be disabled.如果输入字段不为空,复选框将被禁用。
  • If the input field is empty checkbox will be enabled.如果输入字段为空复选框将被启用。

Case Two :案例二

  • If the checkbox checked, Input field will be disabled.如果复选框被选中,输入字段将被禁用。
  • f the checkbox isn't checked, Input field will be enabled.如果未选中该复选框,则将启用输入字段。

here is angular html part;这是有角度的 html 部分;

 <form [formGroup]="testForm"> <div> <label for="veggieMustard">show Text</label> <input formControlName="showTextInput" id="showText" type="text" /> </div> <div> <input formControlName="showCheckbox" id="showCheckBox" type="checkbox" /> <label for="showCheckBox">show</label> </div> </form>

Angular Controller:角度控制器:

 import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, FormControl, } from '@angular/forms'; @Component({ selector: 'order-sheet', templateUrl: './test-form.component.html', styleUrls: ['./test-form.component.css'], }) export class TestController implements OnInit { testForm: FormGroup; constructor(private formBuilder: FormBuilder) { this.buildForm(); } ngOnInit() { this.subscribeToShowTextInput(); this.subscribeToShowCheckBox(); } buildForm(): void { this.testForm = this.formBuilder.group( { showTextInput: this.formBuilder.control(null), showCheckbox: this.formBuilder.control(null), } // To Disable or Enable the Checkbox depeneds on the input field status private subscribeToShowTextInput(): void { const showCheckbox = this.orderSheetForm.controls['showCheckbox']; this.orderSheetForm.controls['showTextInput'].valueChanges.subscribe( (value) => { if (value) { showCheckbox.disable(); } else { showCheckbox.enable(); } console.log(value); } ); } // To Disable or Enable the Input field depeneds on the Checkbox status private subscribeToShowCheckBox(): void { const showTextInput = this.orderSheetForm.controls['showTextInput']; this.orderSheetForm.controls['showCheckbox'].valueChanges.subscribe( (value) => { if (value) { showTextInput.setValue(''); showTextInput.disable(); } else { showTextInput.enable(); } console.log(value); } ); }}

I am getting this Error ERROR RangeError: Maximum call stack size exceeded Which happened because the subscription is always updating each other which gets me into infinite loop, if i disable one of the subscription it works fine,我收到此错误ERROR RangeError: Maximum call stack size exceeded发生这种情况是因为订阅总是相互更新,这让我进入无限循环,如果我禁用其中一个订阅,它工作正常,

Do anyone have any good idea to deal with such situation ?有没有人有任何好主意来处理这种情况?

I'm appreciating any suggestions, directing to documentations or article, because i spend days searching, but i did not find something useful to my situation.我很欣赏任何建议,指向文档或文章,因为我花了几天时间搜索,但我没有找到对我的情况有用的东西。

Thank you in advance.先感谢您。

You should pass in the emitEvent: false option when disabling and enabling the controls.在禁用和启用控件时,您应该传入emitEvent: false选项。


import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl } from '@angular/forms';

@Component({
  selector: 'form',
  templateUrl: 'form.component.html',
  styles: [],
})
export class FormComponent {
  form = this.fb.group({
    showCheckbox: new FormControl(),
    showTextInput: new FormControl(),
  });

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.subscribeToShowTextInput();
    this.subscribeToShowCheckBox();
  }

  private subscribeToShowTextInput(): void {
    this.showTextInput.valueChanges.subscribe((value) => {
      if (value) {
        this.showCheckbox.disable({ emitEvent: false });
      } else {
        this.showCheckbox.enable({ emitEvent: false });
      }
    });
  }

  private subscribeToShowCheckBox(): void {
    this.showCheckbox.valueChanges.subscribe((value) => {
      if (value) {
        this.showTextInput.setValue('');
        this.showTextInput.disable({ emitEvent: false });
      } else {
        this.showTextInput.enable({ emitEvent: false });
      }
    });
  }

  get showCheckbox() {
    return this.form.get('showCheckbox');
  }

  get showTextInput() {
    return this.form.get('showTextInput');
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将两个物品并排放置并仅在必要时将它们包裹起来? - How can I place two items next to each other and make both of them wrap only when necessary? jQuery,CSS:两个div,如何使它们彼此相邻显示? - jQuery, CSS: Two divs, how can I make them “show”next to each other? 如何在使两个按钮保持水平居中居中的同时使两个按钮相邻? - How can I make two buttons next to each other while having them remain being horizontally centered to the webpage? 当两个 Html 输入元素在 HTML 中彼此垂直堆叠/重叠时,如何使它们可见和可访问? - How can I make two Html input elements visible and accessible when they are stacked/overlapped vertically on each other in HTML? 如何使 flex 行中相邻的两个元素相互重叠? - How can I make two elements that are next to each other in a flex row go on top of each other? 如何使两个输入字段彼此相邻 - How to get two input fields next to each other 如何在 wordpress 中相邻显示两个输入字段? - how to show two input fields next to each other in wordpress? 我如何左右浮动两个桌子并将它们居中? - How can I float two tables next to each other left to right and center them both? 如果两个 div 不能彼此相邻,我怎样才能 100% 拉伸它们? - If two div cant fit next to each other, how can I stretch them 100%? 如何循环遍历元素列表并在 jQuery 中在它们上切换 class,每次切换之间有延迟? - How can I loop through a list of elements and toggle a class on them in jQuery, with a delay in between each toggle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM