angular - 如何将 formControlName 赋予 FormArray 对象 - Angular 2 (ReactiveFormsModule)

标签 angular angular-reactive-forms formarray

在我的应用程序中,我使用 Reactive Forms 制作了一个表单。在我的应用程序中,他们是一个按钮 Add new Fields 单击此按钮添加新字段。

我可以添加新字段,但无法分配 formControlName

任何人都可以告诉我正确的路径如何将 formControlName 添加到这些动态添加的字段中。

Here is the Plnkr for this.

最佳答案

您有 FormGroup 数组的 formArray

所以使用 formArrayNameformGroupName 的循环与 formControlName(itemDetail, quantity, rate...)

<table formArrayName="salesList">
    <tr>
     <th>Item Detail</th>
     <th>Quantity</th>
     <th>Rate</th>
     <th>Tax</th>
     <th>Amount</th>
    </tr>
    <!--Input controls -->
    <tr  *ngFor="let saleList of salesListArray.controls;let i = index" [formGroupName]="i">
        <td>
             <div class="col-sm-6">
                 <input class="form-control" type="text" placeholder="Item Detail" formControlName = "itemDetail"/>
             </div>
        </td>
         <td>
             <div class="col-sm-6">
                 <input class="form-control" type="text"   placeholder="0" formControlName = "quantity" />
             </div>
        </td>
         <td>
             <div class="col-sm-6">
                 <input class="form-control" type="text"   placeholder="0.00" formControlName = "rate"/>
             </div>
        </td>
         <td>
             <div class="col-sm-6">
                 <input class="form-control" type="text"   placeholder="Select a tax" formControlName = "tax"/>
             </div>
        </td>
         <td>
             <div class="col-sm-6">
                <span>{{saleList.amount}}}</span>
             </div>
        </td>
    </tr>
</table>

Fixed Plunker

另见

关于angular - 如何将 formControlName 赋予 FormArray 对象 - Angular 2 (ReactiveFormsModule),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44697231/

相关文章:

javascript - 从 Angular 4 HTTP 调用获取完整响应(不仅仅是正文)

Angular 6removeAt和trackBy更新formArray但不正确dom

Angular 5 FormArray

angular - 为什么更改事件会在 controlValueAccessor 模糊时触发

angular - 以 react 形式创建表单控制组

angular - 在 Angular 中创建动态 react 表单时无法将变量设置为 FormControl 名称

javascript - 可观察 Angular 2 的多个响应

javascript - 使用 ngFor 循环 Angular 时无法绑定(bind)到 'index'

javascript - 我的 arrayPizzas 有问题,它永远不会进入第一个 if,问题是什么?我想为重复的列表项添加一个 count++

angular - 如何使用初始值 Validators.Required 和 disabled : true using FormBuilder? 初始化 formcontrol