How to dismiss ActionSheet or Alert in code?


#1

Hi,

I need dismiss ActionSheet or Alert in some scenarios like hardware back button pressed,etc.

Popover and Menu can dismiss or close in code , but ActionSheet and Alert are not.

What can I do?


#2

You can dismiss an alert programmatically, you just have to save an instance of the alert and then call the dismiss method.

public currentAlert: Alert;
opt = {...};
let alert = this.alertCtrl.create(opts);
this.currentAlert = alert;

// alert.present() 

public clearAlert(): void {
    this.currentAlert.dismiss();//clears it
}

now you have an exposed method to clear your aert.