【问题标题】:All of my radio buttons in the radio button group get selected when i select on of them?当我选择单选按钮组中的所有单选按钮时,它们都会被选中?
【发布时间】:2020-03-27 01:29:59
【问题描述】:

您好,我有一个 Angular 5 项目。在其中一个组件中,我有一个 3 个单选按钮,当我选择其中一个时,它们都会被选中。 不知道发生了什么。

在我选择内部单选按钮后,所有单选按钮都会被选中,如下所示。

以下是我的formgroup声明component.ts文件

this.formGroup = this.fb.group({
  accountType: [
    { value: 'INTERNAL', disabled: !this.isTypeEditable },
    [Validators.required]
  ],
  name: ['', [Validators.required]],
  firstName: [
    { value: '', disabled: !this.showAdminFields },
    [Validators.required]
  ],
  lastName: [
    { value: '', disabled: !this.showAdminFields },
    [Validators.required]
  ],
  email: [
    { value: '', disabled: !this.showAdminFields },
    [Validators.required, Validators.email]
  ],
  address1: [{ value: '', disabled: true }, [Validators.required]],
  address2: [{ value: '', disabled: true }, []],
  address3: [{ value: '', disabled: true }, [Validators.required]],
  address4: [{ value: '', disabled: true }, []],
  country: [{ value: '', disabled: true }, [Validators.required]],
  addressProfile: ['', Validators.required],
  features: this.fb.array([]),
  inventories: this.fb.array([]),
  customerId: ['',[strictAlphaNumericValidator]],
  contractId: ['',[strictAlphaNumericValidator]],
  zipCode: ['',[zipCodeValidator]],
  customerContactNumber: ['',[alphanumericValidator]],
  budget:[{value:false, disabled: false},[]],
  messagePrefix: [ '', [messagePrefixEndsWithSpaceValidator]],
  editMessagePrefix: [ { value: false, disabled: false },[]]
});

以下是组件的html代码。

 <pc-form-fields-group [label]="'LABELS.TYPE' | translate" [required]="true">

          <pc-custom-control [label]="'LABELS.INTERNAL' | translate" [inline]="true">
            <input formControlName="accountType" type="radio" [value]="INTERNAL">
          </pc-custom-control>

          <pc-custom-control [label]="'LABELS.EXTERNAL' | translate" [inline]="true">
            <input formControlName="accountType" type="radio" [value]="EXTERNAL">
          </pc-custom-control>

          <pc-custom-control [label]="'LABELS.SERVICE_PROVIDER' | translate" [inline]="true">
            <input formControlName="accountType" type="radio" [value]="SERVICE_PROVIDER">
          </pc-custom-control>

        </pc-form-fields-group>

不确定发生了什么,这对我来说看起来不错。请帮助解决此问题。

谢谢

【问题讨论】:

    标签: angular


    【解决方案1】:

    将 [值] 更改为值

       <pc-form-fields-group [label]="'LABELS.TYPE' | translate" [required]="true">
    
                  <pc-custom-control [label]="'LABELS.INTERNAL' | translate" [inline]="true">
                    <input formControlName="accountType" type="radio" value="INTERNAL">
                  </pc-custom-control>
    
                  <pc-custom-control [label]="'LABELS.EXTERNAL' | translate" [inline]="true">
                    <input formControlName="accountType" type="radio" value="EXTERNAL">
                  </pc-custom-control>
    
                  <pc-custom-control [label]="'LABELS.SERVICE_PROVIDER' | translate" [inline]="true">
                    <input formControlName="accountType" type="radio" value="SERVICE_PROVIDER">
                  </pc-custom-control>
    
                </pc-form-fields-group
    
    >
    

    【讨论】:

    • 是的,很有效。我很快就会批准答案,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    相关资源
    最近更新 更多