当选择值改变Ionic 3时调用方法

时间:2018-04-20 13:06:23

标签: angular ionic3

HTML

    <ion-select interface="popover" [(ngModel)]="selectV">
      <ion-option *ngFor="let human of humans" [value]="v.id">{{v.name}}</ion-option>
      <ion-option onChange="openModal()">GO TO THE MODAL</ion-option>
    </ion-select>
  </ion-item>

这是方法

  openModal() {
    console.log('working')
    this.modalCtrl.create(MyModal).present();
  }

我想尽快在Salect中“转到模态”,它会调用打开模态页面的方法。

2 个答案:

答案 0 :(得分:2)

您可以将[(ngModel)]分为[ngModel](ngModelChange)

<强> component.ts

class Component {
  selectV: any;

  onChange(value: any) {
    this.selectV = value;
    if (value === 'openModal') {
      this.openModal()
    }
  }
}

<强> component.html

<ion-select interface="popover" [ngModel]="selectV" (ngModelChange)="onChange($event)>
  <ion-option *ngFor=" let human of humans " [value]="v.id ">{{v.name}}</ion-option>
  <ion-option [value]='openModal'">GO TO THE MODAL</ion-option>
</ion-select>

答案 1 :(得分:0)

您可以收听ionChange事件:

<ion-select #MySelect ionChange="onChange(MySelect.value)"> 
   <ion-option *ngFor="let human of humans" [value]="v.id">{{v.name}}</ion-option>
   <ion-option value="goToModal">GO TO THE MODAL</ion-option>
</ion-select>

当所选值正确时,打开你的模态:

onChange(value) {
  if (value ==='goToModal') {
    this.openModal();
  };
}