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

时间:2017-06-22 10:49:08

标签: angular angular-reactive-forms formarray

在我的申请表中,我使用Reactive Forms创建了一个表单。在我的应用程序中,单击此按钮时,按钮Add new Fields会添加新字段。

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

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

Here is the Plnkr for this.

1 个答案:

答案 0 :(得分:4)

您拥有formArray数组的FormGroup

因此,formArrayName循环使用formGroupNameformControlName(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

另见