【发布时间】:2019-02-19 08:28:36
【问题描述】:
新功能:
所以我可以让它在没有无线电组或没有正确绑定的情况下工作。我无法让它与两者一起使用。
这是允许的:
<div *ngFor="let column of row.controls.columns.controls; let j = index">
<ion-list radio-group [formControlName]="'col'+j">
<ion-radio></ion-radio>
</ion-list>
</div>
但是我没有带有广播组的列表,无论我更改什么,它都会中断。如果我将列表移出它会中断,请移动它会中断的 formControlName。我真的不知道如何让它工作。
我认为这应该是要走的路:
<ion-list radio-group>
<div *ngFor="let column of columns; let j = index">
<div [formControlName]="'col'+j">
<ion-radio></ion-radio>
</div>
</div>
</ion-list>
但同样的错误:
错误:没有路径的表单控件的值访问器:'rows -> 0 -> col0'
i = 我需要的行数。 j = 我需要的选项/问题的数量。
旧:
我正在尝试使用 ion-radio 作为表单组中的输入字段,但它一直给我错误:
没有路径的表单控件的值访问器...
所以我切换到 type="radio" 的输入,但如果我想对它们进行分组,我需要给它们相同的名称,因为我使用的是 *ngFor,否则我的 formControlName 是不再正确。
谁能帮我让离子无线电工作?分组会更好更容易。
<ion-col radio-group class="radioGroup" col-6>
<ion-col *ngFor="let column of columns; let j = index">
<input value="X" formControlName="col{{j + 1}}" type="radio" />
<!-- <ion-radio value="X"></ion-radio> -->
</ion-col>
</ion-col>
创作:
this.inspectionTableForm = this.formBuilder.group({
header: this.formBuilder.group({
title: [this.question.properties.header[0]],
columns: this.formBuilder.array([
this.formBuilder.control('')
]),
comments: ['']
}),
rows: this.formBuilder.array([
this.initRow()
])
});
private initRow() {
// Create the temp control
let tempControl = this.formBuilder.group({
title: '',
});
// Create an array for the columns
let arr = [];
for (let index = 0; index < this.question.properties["number-of-columns"]; index++) {
// Push a control to the array and also one for the value on a higher level
arr.push(this.columns[index])
tempControl.addControl(`col${index}`, this.formBuilder.control('')) // Col-1-2-3
};
tempControl.addControl('columns', this.formBuilder.array(arr)) //Array
// Check if we need to add the comment field
if (this.question.properties["comment-field"]) {
tempControl.addControl('comment', this.formBuilder.control(''))
}
return tempControl;
}
编辑半工作新代码(感谢 xrobert35):
<ion-col radio-group [formControlName]="'col'+i">
<ion-item *ngFor="let column of row.controls.columns.controls">
<ion-radio></ion-radio>
</ion-item>
</ion-col>
这是我按第一个选项,然后是第二个,然后是第三个选项时得到的结果:
它用一些奇怪的值更新同一个 col。我认为问题在于我不需要 i 作为索引而是 j,但是当我这样做时它会中断。像这样的:
<ion-col radio-group >
<ion-item *ngFor="let column of row.controls.columns.controls; let j = index" [formControlName]="'col'+j">
<ion-radio></ion-radio>
</ion-item>
</ion-col>
表单控件没有值访问器,路径为:'rows -> 0 -> col0'
【问题讨论】:
-
什么错误?我没有看到任何错误消息
-
在 OP 中更新
标签: angular ionic-framework angular-reactive-forms