在Angular / Ionic 2中获取选定的ngModel值?

时间:2017-07-05 22:39:01

标签: angular ionic-framework ionic2

如何使用Angular / Ionic 2在构造函数/函数中获取所选选项?

<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>

我试过这个:

export class ProductPage {
    qty: any;

    constructor(public navCtrl: NavController) {

    }

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

1 个答案:

答案 0 :(得分:0)

<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>

试试这个

export class ProductPage implements OnInit{
    qty: any;

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

请阅读:https://angular.io/guide/lifecycle-hooks