Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close all ionic 2 modals?

I need to close all current modal popups and log out the user in ionic 2 application when the device goes to idle.

I used following methods to close popups in the home component.

this.viewController.dismiss().then(_ => {
  console.log("modal dismiss");
}).catch(error => {
  console.log(error)
});

and

this.navController.popAll().then(_ => {
  console.log("modal dismiss");
}).catch(error => {
 console.log(error);
})

But it throws following error You can't remove all pages in the navigation stack. nav.pop() is probably called too many times.

and does not close any popup. Anyone knows how to do it?

like image 893
Ajantha Bandara Avatar asked Jan 04 '23 21:01

Ajantha Bandara


2 Answers

viewController.dismiss() only closes the current Modal. Meaning you will have to keep a reference to all open modals in and call dismiss() on each one.

You can set use navController.setRoot(LoginPage) (link) to show the login-page.

like image 111
robbannn Avatar answered Jan 06 '23 11:01

robbannn


This worked for me

  constructor(public navCtrl: NavController,public ionicApp: IonicApp){} 

  this.viewCtrl.dismiss().then(_=>{
  let activePortal = this.ionicApp._modalPortal.getActive()
  if (activePortal) {
    activePortal.dismiss(); //can use another .then here
  }
});
like image 34
Leonid Prokopenko Avatar answered Jan 06 '23 11:01

Leonid Prokopenko