问题
I am trying to create a radio button group with different form group name and create a control
my scenario is when i change a select it loads ajax that loads values or to say creates dynamic set of radio button groups
here every thing is fine when select changes groups are loaded and all but the problem occurs when i re change the select say by miss i selected a option based on that the radio button group was created and when i change back to the current one
I assume the controller is duplicating or not creating it gives me the error says
Error: There is no FormControl instance attached to form control element with name: 'action0'
<form [formGroup]="form">
<ion-row class="rows" *ngFor="let detail of details; let i = index">
<ion-col text-center="text-center" col-6="col-6">
{{your_data}}
</ion-col>
<ion-col class="rows last" col-6="col-6">
<ion-list class="row" no-lines="no-lines" radio-group="radio-group" formControlName="action{{i}}">
<ion-item col-4>
<ion-radio class="radio true" mode="md" value="true"></ion-radio>
</ion-item>
<ion-item col-4>
<ion-radio class="radio false" mode="md" value="false"></ion-radio>
</ion-item>
<ion-item col-4>
<ion-radio class="radio " mode="md" value="not sure"></ion-radio>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
<button ion-button (click)="submit()" [disabled]="!form.valid">Submit</button>
</form>
.ts where the select is changing after success of the response i have placed this code
let i;
for(i in this.details){
this.form.setControl('action'+i, new FormControl(null, Validators.required));
}
回答1:
I found the answer here it was dynamically creating form elements and when changed that was still present in the form and was not removed when second time select was changed
Just drop a comment i vl help if you are facing the same issue
来源:https://stackoverflow.com/questions/53031872/ionic-set-control-form-builder-dynamic-form-control-name-binding