如何在ngFor(嵌套formArray)中的ngFor中访问formControlName?

时间:2018-04-22 03:12:27

标签: angular ngfor angular-formbuilder

我是Angular 2的初学者。我正在尝试使用动态表单创建一个表,创建表并且值也绑定但是我无法访问formControlName中的值,尝试下面的代码,但它不起作用。< / p>

有人可以帮我解决我在下面的代码中出错的地方

HTML

<tbody formArrayName="timetable">
     <tr *ngFor="let child of timetableForm.controls.timetable.controls; let i = index;" [formGroupName]="i">
        <td *ngFor="let period of child.controls.periods.controls;let j = index;" [formGroupName]="j">
            {{period.controls.day.value}} <!-- bind is working fine -->
            <input type="text" [formControlName]="day">  <!-- error -->                               
        </td>
     </tr>
</tbody>

TS

ngOnInit(){
this.tableForm = this.formBuilder.group({
      timetable: this.formBuilder.array([
             this.formBuilder.group({
                    periods: this.formBuilder.array([
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Monday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Tuesday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Wednesday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Thursday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Friday'
                        })
                    ])
                }),
                this.formBuilder.group({
                    periods: this.formBuilder.array([
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Monday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Tuesday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            day:'Wednesday'
                        }),
                        this.formBuilder.group({
                            subject_id:10,
                            timing_id:11,
                            name:'Thursday'
                        })
                    ])
                })
            ])
         });

提前致谢。

1 个答案:

答案 0 :(得分:1)

我注意到第一件事

this.formBuilder.group({
      subject_id:10,
      timing_id:11,
      name:'Thursday'
      ^^^^
 it should be day
})

其次,为什么在组件中创建timetableForm时在模板中使用tableForm

第三,您应该使用formControlName="day"代替[formControlName]="day",如评论中所述。

最后,您忘记将子数组包装在formArrayName="periods"中。为此,您可以使用ng-container,如下所示:

<table [formGroup]="tableForm">
    <tbody formArrayName="timetable">
        <tr *ngFor="let child of tableForm.controls.timetable.controls; let i = index;" [formGroupName]="i">
            <ng-container formArrayName="periods">
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       add this
                <td *ngFor="let period of child.controls.periods.controls;let j = index;" [formGroupName]="j">
                    {{period.controls.day.value}}
                    <input type="text" formControlName="day">
                </td>
            </ng-container>
        </tr>
    </tbody>
</table>

<强> Ng-run Example