Get selected ngModel value in Ionic?


#1

I’m trying to get the value of the selected option, but I have no idea how I’m supposed to do that. Literally I spent 2-3 hours looking all over the internet. I just want to get the quantity when the Buy Now button is clicked.

HTML:

<ion-item>
    <ion-label>Quantity</ion-label>
    <ion-select [(ngModel)]="qty">
        <ion-option value="1" selected="true">1</ion-option>
        <ion-option value="2">2</ion-option>
        <ion-option value="3">3</ion-option>
    </ion-select>
</ion-item>

<button ion-button full (click)="addToCart()">Buy Now</button>

TS:

export class ProductPage {
    qty: any;

    constructor(public navCtrl: NavController) {

    }

    addToCart() {
        console.log(this.qty);
    }
}

#2

qty should be a string instead of any, but your code works fine for me. I choose 2 from the menu, I press “buy now”, and the console shows “2”.


#3

Aaah I realized where’s the problem! It returns undefined if it’s automatically selected. I’ll just set the first option to “Select Quantity” and use a check function. Thanks anyway!