Add item in dynamic reactive form in Angular

后端 未结 2 429
鱼传尺愫
鱼传尺愫 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:44

    So first let start with understanding what you do.

    You make a formarray in formarray. So this mean it have two loops. So you have to *ngFor the first and *ngFor the second. In the end this means you have an index i for your items and and j for your subItems. Then you can add addSubItems(i) for pushing your subItems.

    Your html should look something like this:

    <-- here your button -->

    Your addSubItems should look something like:

     addSubItems(_index: any): void {
        this.item = this.orderForm['controls']['items']['controls'][_index] as FormGroup;
        this.subItems = this.item.get('subItems') as FormArray;
        this.subItems.push(this.createSubItem());
    

提交回复
热议问题