Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 防止多个离子警报堆积_Ionic Framework_Ionic2 - Fatal编程技术网

Ionic framework 防止多个离子警报堆积

Ionic framework 防止多个离子警报堆积,ionic-framework,ionic2,Ionic Framework,Ionic2,如何检测ionic 2实例是否已打开,以便不显示另一个警报?我最后为ionic的警报控制器编写了一个包装提供程序,如下所示: import { Injectable } from '@angular/core'; import { AlertController } from 'ionic-angular'; @Injectable() export class Alert { public alertPresented: any; constructor(public alertCt

如何检测ionic 2实例是否已打开,以便不显示另一个警报?

我最后为ionic的警报控制器编写了一个包装提供程序,如下所示:

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

您可以创建一个
AlertService
以使用更多选项处理该问题,而无需为按钮插入事件

从'@angular/core'导入{Injectable};
从“离子角度”导入{AlertController,Alert};
/**
*一个简单的警报类,在同一时间只显示一个警报
*/
@可注射()
导出类警报服务{
当前警报:警报
构造函数(专用alertCtrl:AlertController){
}
显示(标题、消息、按钮:any=[],输入:any=[],cssClass=''){
如果(!buttons.length){
按钮。按下('确定')
}
让alertOptions:任意={
标题:标题,,
字幕:信息,,
按钮:按钮,
cssClass:buttons.length==2?“确认文件”:cssClass
}
if(输入。长度){
alertOptions.inputs=输入
}
如果(!this.currentAlert){
this.currentAlert=this.alertCtrl.create(alertOptions)
this.currentAlert.present()
此.currentAlert.OnDidDisclose(()=>{
this.currentAlert=null
})
}
返回此.currentAlert
}
}

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

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

我的解决方案奏效了,我必须在取消事件后设置一个布尔值并将其设置为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
    }
  }

这只会阻止创建新警报。这不会自动取消旧警报并显示最新警报,对吗?关于如何自动取消旧警报并显示新警报,有什么想法吗?我已经为此提出了一个问题:请在答案中插入您的注释,或在代码中添加上下文,并解释它的作用。请看这个:看看我的意思。