Prevent multiple Ionic Alerts from stacking up

前端 未结 4 1297
孤城傲影
孤城傲影 2021-02-20 06:37

How can I detect if ionic 2 alert ui component instance is already open in order not to present another alert ?

4条回答
  •  难免孤独
    2021-02-20 07:23

    I have another idea that you can assign message for a variable and check new message is equal to it or not. If equal, return. This is my code and hope you enjoy with it.

    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()
      }
    }
    

提交回复
热议问题