Prevent multiple Ionic Alerts from stacking up

前端 未结 4 1274
孤城傲影
孤城傲影 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:33

    I ended up writing a wrapping provider for Ionic's Alert controller like so :

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

    where alertPresented flag prevents more than one instance from being presented

提交回复
热议问题