【发布时间】:2021-10-07 02:30:01
【问题描述】:
我正在 Angular 中创建响应式表单。在其中,我试图在我现有的FormBuilder 中使用embedded FormArray。以下是代码详情。
组件文件中的逻辑
this.addLoadTypeForm = this.formbuilder.group({
'loanDescription': new FormControl('', [
Validators.required,
Validators.maxLength(20)]),
users: new FormArray([
new FormControl('test'),
new FormControl('Testing')
])
})
HTML 代码
<form [formGroup]="addLoadTypeForm">
<table>
<tr>
<td>
<div class="form-group">
<label>
descriptions
</label>
<textarea formControlName='loanDescription'></textarea>
</div>
</td>
</tr>
<tr>
<td>
<div class="form-group" formArrayName="users">
<label>Users</label>
<div *ngFor="let control of addLoadTypeForm.controls.users['controls'];let i=index">
<!-- <input type="text" [formControlName]="i"> -->
</div>
</div>
</td>
</tr>
</table>
</form>
我遇到的错误
元素implicity 有一个
any类型,因为类型AbstarctControl有 没有索引签名。您的意思是调用“get”吗?
PS:我也试过“让控制 addLoadTypeForm.get('control')['controls']; let i = index;”仍然不适合我
【问题讨论】:
-
按照上面链接中提到的步骤后,我没有收到错误,但现在我无法在屏幕上看到绑定的数据。
-
这个例子可能对你有帮助link
-
使用 formBuilder 添加一个 FormArray 使用 this.formbuilder.array,添加一个 FormControl 你不使用新的 FormControl:stackoverflow.com/questions/68470645/…
标签: angular formbuilder formarray form-control formgroups