Dismiss all overlay components

I am building an Ionic 2 app using ngrx, but I have a problem in dismissing overlay components.

To login I show the user a modal with a login-form. This dispatches an action which is handled not in the page but somewhere else. If the login was successful I want to set a new root-page and dismiss all overlay components (in this case the modal). This cannot be done from within the modal component since all the logic is handled somewhere else.

I already tried: clearing the whole navigation stack, calling popToRoot() and some other things but nothing worked. The is not dismissed.

Is there a way to dismiss all the modals/page except the root-page? I would want to have something like this:

this.appCtrl.getRootNav().dismissOverlays();

Did you get solution for this. I have the same problem. Modal popup, ionic select-options modal are not dismissing when device screen lock

I am currently using the following workaround:

private dismissAllPages(){
if(this.appCtrl.getActiveNav() && this.appCtrl.getActiveNav().length() > 0 ){
return this.appCtrl.getActiveNav().popToRoot();
}
}

But sometimes, in dev mode, I get an exception, so it is not optimal…

Create a service that you inject in all of the classes that use the modals. Use the service to display modals and push into an array. Use the service to iterate through the array to dismiss all modals that were created.

I had the same problem and solved it using Events.

On the login modal, you subscribe to an event, for example “modals:close”, on which you call this.dismiss().

On the page where you navigate to from the modal, you just fire the event.

1 Like

Hello. Can you please give code sample of how to implement this. I’m in the same boat.

Is it something like this:


//modal page
this.events.subscribe('alreadyloaded', (l)=>{
     this.viewCtrl.dismiss();
    });

//Origin page
this.events.publish('alreadyloaded', true);

Yes, exactly.
I use a convenience method that I have in my utils class:

  closeAllModals(){
    this.events.publish('modals:close');
  }

The big benefit of using event for closing modals is that you can publish the event from anywhere, any class, any context, and the subscribed modal will catch it.

For the sake of completeness, the exact code I used for subscribing to the event is:

    this.events.subscribe('modals:close', () => {
      this.dismiss();
    })

in my modal page. I don’t remember why I didn’t have to go through the viewController to dismiss it, you’d probably find that in API reference.

is it working?
I thought it will work in ionic 4, because it seem quite accurate, but not working in ionic 4

 this.events.subscribe("modals:close", () => {
      console.log("On nootufy 2");
      this.modalCtrl.dismiss();
    });

console.log – is logging when I am publishing the event but the modal popup is not closing.

I am seeing the same thing as Thesurya9
In the subscribe callback this.modalctrl.dismiss() is not dismissing the modal.

"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"@ionic/angular": "^4.4.0",

The solution is to call the modalController dismiss function after onDidDismiss promise resolves on the calling page! This works.

2 Likes