Add item in dynamic reactive form in Angular

后端 未结 2 432
鱼传尺愫
鱼传尺愫 2020-12-19 22:04

I am trying to build a nested reactive form in Angular 5. I am able to push items at one level, but unable to push item at second level.

ngOnInit() {
 this.         


        
2条回答
  •  再見小時候
    2020-12-19 22:47

    To reduce amount of code, these can little refactored addSubItems(index) to addSubItems(item):

    ...
    ...
    ...
    addSubItems(item: FormGroup): void { this.subItems = item.get('subItems') as FormArray; var newSubItem = { 'subname': 'value6' } this.subItems.push(this.createSubItem(newSubItem)); }

    So, no need to find item by index: var a = this.orderForm['controls']['items']['controls'][_index] as FormGroup;, because you are already can pass item: FormGroup

    StackBlitz Demo

提交回复
热议问题