Dynamic form in ionic

Hey everyone, I have a dynamic form that changes depending of a param that comes from the database, so i need to catch de data that the user puts in the different inputs, so any idea how could I bind the users answers ?

Hi @josechepe, If you have some input fields and a button on which you want to store/catch values you entered in input fields then here is the solution, It worked for me:

<ion-input type="text" placeholder="Whatever you want" [(ngModel)]="SomeName"></ion-input>

then on your button click, you can catch values as:

<button ion-button (click)="yourFunction(SomeName)">Pass Values</button> 

Now you can write a function in .ts file named as yourFunction(someParamName) and catch your value in its parameter. You can send more than one values comma separated and can get in your function respectively and can do whatever you want to. Hope it helps. Please let me know if you find any confusion or the solution doesn’t work.

Hey man, thaks for replying.

I found other way creating a new attribute to each objetc I get from the database, so I created the form using those elements so I have to bind like this [(ngModel)]=data.reply, so reply is the new attribute in the object.

the HTML

<ion-spinner class="center" *ngIf="loading"></ion-spinner>
      <div *ngFor="let params of paramsPage">
        <!-- type LISTA -->
        <ion-item class="input" *ngIf="params.description == 'LISTA'">
            <ion-label color="light">{{params.name}}</ion-label>
            <ion-select [(ngModel)]="params.reply">
              <ion-option  *ngFor="let list of params.lista">{{list.value}}</ion-option>

          <!-- type NUMBER  -->
          <ion-item class="input" *ngIf="params.description == 'NUMBER'">
              <ion-label floating color="light">{{params.name}}</ion-label>
              <ion-input type="tel" [(ngModel)]="params.reply"></ion-input>

the .ts

 this.rest.reportList(element.sql_dynamic).then(datos => {
              element["reply"] = "";


1 Like

Hi @umerf6455 I am having the same kind of issue. I tried your solution but the ngModel binds the ion input text all together. please how can I fix this? Thanks

<form (ngSubmit)="submitResponse(names)">
            <div *ngFor="let questionnaire of questionnaires;" >
                <div *ngFor="let item of questionnaire.item;" >
                   <ion-label> {{ item.prefix }} &nbsp; {{ item.text }} </ion-label>  
                       <div *ngIf="item.type == 'boolean'" >
                            <ion-label>{{ item.option.valueBoolean[0] }}</ion-label>
                            <ion-radio  ></ion-radio>
                            <ion-label>{{ item.option.valueBoolean[1] }}</ion-label>
                            <ion-radio ></ion-radio>
                       <div *ngIf="item.type == 'string'" >
                            <ion-input type="text" [(ngModel)]="names" name="names"> </ion-input>

and the ts file

selector: ‘questions’,
templateUrl: ‘questions.html’
export class QuestionsPage implements OnInit {

 questionnaires: any; 

public navCtrl: NavController,
private userapi: UserApi,
private questionnaireapi: QuestionnaireApi,
private questionnaireresponseapi: QuestionnaireResponseApi
{ }

this.questionnaires = data;


this.questionnaires = data;