Ionic set control form builder dynamic form control name binding

自作多情 提交于 2019-12-13 03:56:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!