【问题标题】:FormGroup containing Radio Buttons with an Angular FormArrayFormGroup 包含带有 Angular FormArray 的单选按钮
【发布时间】:2018-06-23 10:16:15
【问题描述】:

我有以下代码动态添加包含标题和两个单选按钮的表单组以创建条件,有效地提供其标题以及它是否是可接受的条件:

export enum Acceptability {
  ACCEPTABLE,
  INACCEPTABLE
}

export interface Condition {
  title: string;
  acceptability: Acceptability;
}

export class AddCondition implements OnInit {

  form: FormGroup;
  ACCEPTABILITY = Acceptability;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    // build the form
    this.form = this.fb.group({
      conditions: this.fb.array([])
    });

  }

  get conditions(): FormArray {
    return this.form.get('conditions') as FormArray;
  }

  addCondition() {
    this.conditions.push(this.fb.group({
      title: ['', Validators.required],
      acceptability: ['', Validators.required]
    }));
  }
}
<div formArrayName="conditions">
  <div *ngFor="let condition of conditions.controls; let i=index" [formGroupName]="i">
    <input class="form-control" formControlName="title" type="text" placeholder="title">
    <div class="custom-control custom-radio custom-control-inline">
      <input type="radio" id="acceptable" formControlName="acceptability" name="acceptability" class="custom-control-input" [value]="ACCEPTABILITY.ACCEPTABLE">
      <label class="custom-control-label" for="acceptable">Acceptable</label>
    </div>
    <div class="custom-control custom-radio custom-control-inline">
      <input type="radio" id="inacceptable" formControlName="acceptability" name="acceptability" class="custom-control-input" [value]="ACCEPTABILITY.INACCEPTABLE">
      <label class="custom-control-label" for="inacceptable">Inacceptable</label>
    </div>
  </div>
  <button type="button" class="btn btn-outline-dark btn-block" (click)="addCondition()">
          <i class="fa fa-plus fa-lg mr-3"></i>ADD A NEW CONDITION</button>
</div>

问题是当点击ADD A NEW CONDITION按钮时,出现新的表单组,每次我选择组数组中的任何单选按钮时,组中第一个数组项的对应单选按钮就是那个被选中。

【问题讨论】:

  • 仅供参考 un 可以接受
  • 哈哈!真的;问题是我将所有内容都从法语重命名,实际上忽略了这一点:)
  • 控制台有错误吗?听起来好像有什么东西坏了
  • 绝对不是。

标签: angular angular-reactive-forms angular-forms reactive-forms


【解决方案1】:

循环遍历数组组时需要考虑的是分配给每个组的idname。目前,您的输入 idname 在每个循环中都设置为相同的值。

您需要使用可用的i 变量来使每个输入的idname 成为动态值,如下所示:

<div [formGroup]="form">
  <div *ngIf="conditions" formArrayName="conditions">

    <div *ngFor="let condition of conditions.controls; let i=index" [formGroupName]="i">

      <input class="form-control" formControlName="title" type="text" placeholder="title">

      <div class="custom-control custom-radio custom-control-inline">
        <input type="radio" id="{{ 'acceptable' + i}}" formControlName="acceptability"
            class="custom-control-input" [value]="ACCEPTABILITY.ACCEPTABLE">
        <label class="custom-control-label" for="{{ 'acceptable' + i}}">Acceptable</label>
      </div>

      <div class="custom-control custom-radio custom-control-inline">
        <input type="radio" id="{{ 'unacceptable' + i}}" formControlName="acceptability"
            class="custom-control-input" [value]="ACCEPTABILITY.INACCEPTABLE">
        <label class="custom-control-label" for="{{ 'unacceptable' + i}}">Unacceptable</label>
      </div>

    </div>

    <button type="button" class="btn btn-outline-dark btn-block" (click)="addCondition()">
            <i class="fa fa-plus fa-lg mr-3"></i>ADD A NEW CONDITION</button>
  </div>
</div>

<div>
  <pre>{{ form.value | json }}</pre>
</div>

注意nameid 属性的字符串插值。 for 标签中的 for 属性也是如此。

您可能不得不玩弄它才能得到它需要的确切位置。 (我没有彻底测试过)

希望这会有所帮助。

【讨论】:

  • 谢谢,但这会产生以下错误:If you define both a name and a formControlName attribute on your radio button, their values must match. Ex: &lt;input type="radio" formControlName="food" name="food"&gt;
  • 但是当然,如​​果我继续更改formControlName 以匹配它,那么我会收到控件不存在的错误;这是正常的,因为当我推送新组时,我只用名称acceptability 推送它。
  • 您是否尝试过不设置name 属性,将其移除?
  • 好的,让我试试看。当我有东西时,我会更新答案。来回抱歉!
  • 确实;我想删除名称和 not 具有formControlName 动态是我没有尝试的一种排列。谢谢你:)
【解决方案2】:

我好像错过了这条线 id="{{ 'unacceptable' + i}}" 用于 id 和 label 两者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2019-02-19
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多