gpt4 book ai didi

Angular FormArray : referencing dynamically added FormControl in template

转载 作者:太空狗 更新时间:2023-10-29 17:26:34 24 4
gpt4 key购买 nike

我正在处理 Angular Reactive 表单,我已将几个 FormControl 动态添加到 FormArray 中,但是在将它们与 formControlName 绑定(bind)时在我的模板中引用这些 FormControl 时遇到问题。在模板中,我将循环索引变量“i”分配给 formControlName,但它不起作用。通过将控件与 formControlName 绑定(bind),我得到“没有路径的表单控件的值访问器”。这是我的组件类代码:

export class Control {
constructor(public controlType?: string, public label?: string, public required?: boolean, public placeholder?: string,
public options?: string[], public value?: string) { }
}

export class FormComponent implements OnInit {
reviewForm: FormGroup;

constructor(public formService: FormService, public dialog: MdDialog) { }


selectedControl: any = null;
label: string;

ngOnInit() {
this.reviewForm = new FormGroup({
'controlArray': new FormArray([
])
});
}

addControl() {
const myControl: Control = new Control(this.selectedControl.name, this.label, this.isRequired,
this.selectedControl.attributes.value, this.selectedControl.attributes.options, 'null');
var control: FormControl = new FormControl(myControl);
(<FormArray>this.reviewForm.get('controlArray')).push(control);
}

}

这是我组件的模板:

<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()">
<div class="example-label">
<span class='block'>
<span *ngIf="selectedForm">
<h2 class="example-heading">{{displayForm}} </h2>
</span>
<div formArrayName="controlArray">
<div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index">
<table>
<tr>
<span *ngIf="control.value">
<td>
<label>{{control.value.label}}</label>
</td>
<td><span *ngIf="control.value.controlType == 'select'">
<md-select placeholder="{{control.value.label}}" [formControlName]="i" >
<md-option *ngFor="let option of control.value.options; let i=index"
[value]="option">{{control.value.options[i]}}</md-option>
</md-select>
</span></td>
<td> <span *ngIf="control.value.controlType == 'text'">
<md-form-field class="example-full-width">
<input mdInput type="text" placeholder="{{control.value.placeholder}}" [formControlName]="i" >
</md-form-field>
</span></td>
<td> <span *ngIf="control.value.controlType == 'radio'">
<md-radio-group>
<span *ngFor="let option of control.value.options">
<md-radio-button name="gender" value="{{option}}" [formControlName]="i" >{{option}} </md-radio-button>
</span>
</md-radio-group>
</span>
</td>
<td> <span *ngIf="control.value.controlType == 'checkbox'">
<md-checkbox value="{{control.value.controlType}}" [formControlName]="i" > </md-checkbox>
</span></td>

<td> <span *ngIf="control.value.controlType == 'textarea'">
<textarea name="Comment" id="" cols="30" rows="10" [formControlName]="i" ></textarea>
</span></td>

<td> <span *ngIf="control.value.controlType == 'button'">
<button md-raised-button value="{{control.value}}" [formControlName]="i" >Button</button>
</span> </td>
</span>
</tr>
</table>
</div>
</div>
</span>
</div>
</form>

在我的组件类中,我在 FormGroup 中只有 FormArray。在 addControl 函数中,我制作了一个自定义控件并将该控件作为第一个参数传递给 FormControl。之后我将这个 FormControl 插入我的 FormArray。请帮助,在模板中如何将 formControlName 关联到 FormControls。谢谢

最佳答案

你可以这样绑定(bind):

reviewForm.controls.controlArray.controls[i].controls.DATAYOULIKEBIND.value

关于 Angular FormArray : referencing dynamically added FormControl in template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675268/

24 4 0