Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i set ion-select value a number with reactive form using form group?

Tags:

angular

ionic2

<ion-select formControlName="FollowUpType"
          (ngModelChange)="reset()">
          <ion-option value="0">
            A
          </ion-option>
          <ion-option value="1">
           B
          </ion-option>
  </ion-select>

I need this select when I take it's value to be number not string (0 number , or 1 number ) not "0" and not "1". I'm using reactive form :

 this.Form = this.formBuilder.group({
      FollowUpType: [''],
   });
like image 726
Joe Sleiman Avatar asked Dec 07 '22 17:12

Joe Sleiman


2 Answers

You should bind as [value] and not value

<ion-select formControlName="FollowUpType" (ngModelChange)="reset()">
    <ion-option [value]="0">A</ion-option>
    <ion-option [value]="1">B</ion-option>
</ion-select>
like image 193
Quethzel Díaz Avatar answered Dec 10 '22 11:12

Quethzel Díaz


What I see here are a couple of options, which both tho requires work, other for styling, other for extra variables. So going for parseInt() before passing the data to the backend would be preferable.

Option one is to just use select instead of ion-select, with that, ngValue works, which can capture the number. But that requires styling for the select to look like an ionic select.

Option two would be to have two variables and use [value] in the select instead of select.

Final option would then be to use an array

values = [{value:0, label:'A'},{value:1, label: 'B'}]

iterate it and use it together with [value]:

<ion-select formControlName="FollowUpType" (ionChange)="reset()">
   <ion-option *ngFor="let val of values" [value]="val.value">
     {{val.label}}
   </ion-option>
</ion-select>

Demo with all three: http://plnkr.co/edit/dwbttQZTh9JGwKcvFtTX?p=preview

Maybe easiest is to just go with parseInt() for the value before sending it to the backend.

like image 21
AT82 Avatar answered Dec 10 '22 13:12

AT82