包含带有Angular FormArray的单选按钮的FormGroup [英] FormGroup containing Radio Buttons with an Angular FormArray
问题描述
我有以下代码,可以动态添加一个包含标题和两个单选按钮的表单组以创建条件,从而有效地提供其标题以及是否可接受的条件:
I have the following code that dynamically adds a form group containing a title and two radio buttons to create a condition, effectively providing its title and whether or not it's an acceptable condition:
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>
问题是,当单击添加新条件"按钮并出现新表单组时,每次我选择组数组中的任何单选按钮时,组中第一个数组项的对应单选按钮就是一个被选中.
The problem is that when clicking the ADD A NEW CONDITION button, and the new form group appears, every time I select any radio button in the group array, the corresponding radio button of the first array item in the group is the one that gets selected.
推荐答案
遍历数组组时需要考虑的是分配给每个数组的id
和name
.当前,您在每个循环中将输入id
和name
设置为相同的值.
What you need to consider when looping through an array group is the id
and name
you assign to each one. You currently have your input id
and name
getting set to the same value in each loop.
您需要使用可用的i
变量,使每个输入的id
和name
为动态值,如下所示:
You would need to use the i
variable you have available to make the id
and name
for each input a dynamic value, like below:
<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>
注意name
和id
属性的字符串插值.标签中的for
for属性也是如此.
Notice the string interpolation for the name
and id
attributes. Same goes for
the for attribute in the labels.
您可能必须尝试使用它才能获得所需的精确位置. (我没有对其进行彻底的测试)
You may have to play around with this to get is exactly where it needs to be. (I didn't test it thoroughly)
希望这会有所帮助.
这篇关于包含带有Angular FormArray的单选按钮的FormGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!