防止多个离子警报堆叠起来

时间:2016-10-09 06:23:47

标签: ionic-framework ionic2

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

4 个答案:

答案 0 :(得分:6)

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

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标志阻止呈现多个实例

答案 1 :(得分:0)

您可以创建一个AlertService来处理更多选项,而无需为按钮注入事件

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

/**
 * A simple alert class to show only one alert at the same time
 */
@Injectable()
export class AlertService {
  currentAlert: Alert
  constructor(private alertCtrl: AlertController) {
  }

  show(title, message, buttons: any = [], inputs: any = [], cssClass = '') {
    if (!buttons.length) {
      buttons.push('Ok')
    }
    let alertOptions: any = {
      title: title,
      subTitle: message,
      buttons: buttons,
      cssClass: buttons.length === 2 ? 'confirmAlert' : cssClass
    }
    if (inputs.length) {
      alertOptions.inputs = inputs
    }
    if (!this.currentAlert) {
      this.currentAlert = this.alertCtrl.create(alertOptions)
      this.currentAlert.present()
      this.currentAlert.onDidDismiss(() => {
        this.currentAlert = null
      })
    }
    return this.currentAlert
  }
}

关于Nicholls

答案 2 :(得分:0)

我有另一个想法,您可以为变量分配消息,并检查新消息是否等于它。如果相等,则返回。 这是我的代码,希望您喜欢它。

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

@Injectable()
export class AlertProvider {

  public showingMessage = ""

  constructor(
    private alertController: AlertController
  ) {}

  showAlert(message) {
    // Check this message is showing or not
    if (message === this.showingMessage) {
      return
    }

    this.showingMessage = message
    this.alertController.create({
      title: "APP_NAME",
      message: message,
      buttons: [{
        text: "OK",
        handler: () => {
          this.showingMessage = ""
        }
      }]
    }).present()
  }
}

答案 3 :(得分:0)

我的解决方案有效,我必须放入一个布尔值,并在取消事件后将其设置为true,并在出现警报时将其设置为false

if (this.network_alert) {
      let alert = await this.alertController.create({
        header: "No Network",
        message:
          "Please check your internet connection",
        buttons: [{
          text: "Dismiss",
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
            this.network_alert = true
          }
        }],

      });
      await alert.present();
      this.network_alert = false
    }
  }