Is there any way to check if a modal is currently being presented?
I tried to use NavController to get the active view, but it gives me the page and not the modal view.
// Create and present the modal somewhere in a component/page
const modal = this.modalCtrl.create(MyModalPage);
modal.present();
// Check if we are presenting a modal in AppComponent
const view = this.navCtrl.getActive();
console.log(view.component === MyModalPage); // gives false
Yes, but that does not help if I want to check if a modal is being presented from another component or service. The modal is presented from a service, and the check is done in the app component.
I see, so there is no official API that can do that? I have a hack in place that is similar to what you suggest, but itâs not really a good solution.
I had a similar issue although with ionic/angular version 1, I just decided to drop the crude solution I came up with, in case someone stumbles on this post like I did when I searched online for the same solution. I was able to check if $ionicModal is opened by checking if the modal $scope variable is shown. Since it returnâs undefined if itâs not showing. Example:
$ionicModal.fromTemplateUrl("modal.html", {
scope: $scope,
animation: "slide-in-up"
}).then(function(modal){
$scope.modal = modal;
$scope.modal.show();
});
if($scope.modal._isShown){ /*_isShown returns boolean value of true if modal is showing otherwise returns undefined */
//
// do some super cool stuff here.......
}
Although, I donât know if this is the most elegant way to achieve this but it worked for me and hope it helps someone!
Do you mean check if a userâs Modal is open, remotely?
Could you not just have a status set on your back-end for each user that toggles on open/Close of the modal. For instance, a âmodal_openâ field, something like that?
If you mean checking if the current user has the modal open, @bengtler has the right idea. Just have a modal_is_open boolean that toggles in a service
Gosh, it has just taken me AGES to find a solution for dismissing a popover on back/forward navigation. I had subscribed to router events to dismiss the popover which worked the first time navigating away from the page, but navigating back or forward again would give an error every time afterwards: âERROR Error: Uncaught (in promise): overlay does not existâ.
I finally fixed it with this code in my popover.component.ts (taken from your suggestion - so thank you very much!):