我如何检测ionic 2 alert ui component实例是否已经打开以便不显示其他警报?

最佳答案

我最终为Ionic的Alert Controller 编写了一个包装提供程序,如下所示:

import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';

@Injectable()
export class Alert {
  public alertPresented: any;
  constructor(public alertCtrl: AlertController) {
    this.alertPresented = false
  }

  present(title, subTitle) {
    let vm = this
    if(!vm.alertPresented) {
      vm.alertPresented = true
      vm.alertCtrl.create({
        title: title,
        subTitle: subTitle,
        buttons: [{
          text: 'OK',
          handler: () => {
            vm.alertPresented = false
          }
        }],
      }).present();
    }
  }
}

其中alertPresented标志阻止显示多个实例

09-25 19:30