Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get selected value from ion-select in Ionic

I am trying to get the selected option Value from ion-select
but when I select any value from ion-select I am getting Undefined value since I am new to ionic
I was unable to sort it on my own.

HTML:

<ion-item>
   <ion-label>Quantity</ion-label>
   <ion-select [(ngModel)]="number"  
     (ionChange)="onChange(carbrand)" >
      <ion-option *ngFor="let count of quantity" 
       [value]="count" >{{count}}</ion-option>
      </ion-select>
   <!-- <ion-select [(ngModel)]="number">
      <ion-option *ngFor="let count of quantity" 
      value="count"></ion-option>
   </ion-select> -->
</ion-item>

Home.ts :

onChange(SelectedValue){ 
  console.log("Selected Quantity", SelectedValue); 
}
like image 417
fiverr freelancer Avatar asked Dec 24 '22 01:12

fiverr freelancer


1 Answers

You can also use two other approaches.

1 - Pass $event to your function:

Html:

<ion-item>
    <ion-label>Quantity</ion-label>
    <ion-select [(ngModel)]="number" (ionChange)="onChange($event)" >
      <ion-option *ngFor="let count of quantity" value="count"></ion-option>
    </ion-select>
    <!-- <ion-select [(ngModel)]="number">
      <ion-option *ngFor="let count of quantity" value="count"></ion-option>
    </ion-select> -->
</ion-item>

Ts:

onChange(value){
  console.log(value);
}

2 - Use an Id in your select element:

Html:

<ion-item>
    <ion-label>Quantity</ion-label>
    <ion-select #S [(ngModel)]="number" (ionChange)="onChange(S.value)" >
      <ion-option *ngFor="let count of quantity" value="count"></ion-option>
    </ion-select>
    <!-- <ion-select [(ngModel)]="number">
      <ion-option *ngFor="let count of quantity" value="count"></ion-option>
    </ion-select> -->
</ion-item>

Ts:

onChange(value){
  console.log(value);
}

Hope it helps!

like image 151
Leonardo Gabriel Avatar answered Jan 07 '23 15:01

Leonardo Gabriel