在嵌套 Angular formGroups 中动态分配 formControlName

Assigning formControlName dynamically in nested Angular formGroups

我想使用 formGroupformArray 在动态创建的 input 表单上设置 formControlNameplaceholder

component.html :

 <form [formGroup]="ecosystemFormG" novalidate>  
        <div  formArrayName="transactions">
            <div  [formGroupName]="i" *ngFor="let transaction of getTransactions(ecosystemFormG); let i = 'index'">
                <div>
                    <!-- Dropdownselect for transactionVintage  Calling methd (transactionVintageMethod)-->
                </div>
                <div formArrayName="transactionVintageValues">
                    <div   [formGroupName]="j" *ngFor="let value of getTransactionVintage(transaction); let j = 'index'">
                        [[ value.placeholder ]] -->not printing anything
                                        [[ value.ctrlname ]]-->not printing
                                        [[ j ]] [[value]]
                                        <mat-form-field color="accent">
                                            <input matInput [placeholder]="value.placeholder" [formControlName]="value.ctrlname">  
                                        </mat-form-field>  <--throwing error  -->

                    </div> 
                </div>
            </div>
        </div>  
</form>


 [[ecosystemFormG.value | json]]

这是 Json 正在创建 : [[ecosystemFormG.value | json]]

{"transactions": [
        { 
          "transactionVintage": 3, 
          "transactionVintageValues": [
            {
              "ctrlName": "ctrlname_1",
              "placeholder": "plcaholder1"
            },
            {
              "ctrlName": "ctrlname_2",
              "placeholder": "plcaholder2"
            },
            {
              "ctrlName": "ctrlname_3",
              "placeholder": "plcaholder3"
            }
          ]
        }
      ]
}

component.ts

    export class EcosystemComponent implements OnInit {

    ecosystemFormG: FormGroup;

    getTransactions(form) { 
      return form.controls.transactions.controls;
    } 

    getTransactionVintage(form) { 
      return form.controls.transactionVintageValues.controls;
    }

    transactionVintageMethod(_event, _transactionForm, i){ 
        let _ctrls = this.getTransactionVintage(_transactionForm); 
        this.remove(i);
        for(var l=1; l<=_event.value; l++) { 
            this.add(i,l);
        }
    }

    remove(i){
         // remove all input fields 
    }

    add(i,l) { 
      const control = <FormArray>this.ecosystemFormG.get('transactions')['controls'][i].get('transactionVintageValues');
      //some unknown logic to put formConetrolName and placeholder
      control.push(this.initTransactionVintageValues(l));
    }

    initTransactions() {
        return new FormGroup({ 
          transactionVintage: new FormControl(''), 
          transactionVintageValues: new FormArray([])
        });
    }

    initTransactionVintageValues(l) { 
        let ctrlname = "ctrlname_" + l;
        let plcaholder =  "plcaholder" + l

       return new FormGroup({
         ctrlName: new FormControl(ctrlname), 
         placeholder : new FormControl(plcaholder)
       });
    }

    ngOnInit() { 
        this.ecosystemFormG = new FormGroup({ 
          transactions: new FormArray([
            this.initTransactions(),
          ]),
        });
    }
}

我在 json 中的 transactionVintage 键是 html 数字中的下拉选择器,它告诉要创建的输入字段的数量。

在这里,我可以创建动态输入字段。

所以问题是我们如何为动态创建的输入字段分配 formControlName 和 plcaholder。

为什么不直接使用普通绑定?

<div   [formGroupName]="j" *ngFor="let value of getTransactionVintage(transaction); let j = index">
    <mat-form-field color="accent">
        <input matInput [placeholder]="value.placeholder" [formControlName]="value.ctrlname"> 
    </mat-form-field> 
</div> 

此外,为什么 'index'*ngFor 中有单引号?那里不需要单引号。

*ngFor="let item of items; let i = index"

*ngFor="let user of users; index as i"