【问题标题】:Error when including FormArray in FormBuilder在 FormBuilder 中包含 FormArray 时出错
【发布时间】: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;”仍然不适合我

【问题讨论】:

标签: angular formbuilder formarray form-control formgroups


【解决方案1】:

FormArray 中的表单控件未命名,您可以将它们与 formControl 一起使用,而不是 formControlName

<div *ngFor="let control of addLoadTypeForm.controls.users['controls'];let i=index">
     <input type="text" [formControl]="control">
</div>

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 1970-01-01
    • 2018-02-08
    • 2022-01-23
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2015-09-17
    相关资源
    最近更新 更多