简体   繁体   中英

How to prevent modal dismiss when clicked outside the modal in Ionic?

I'm building a simple mobile app that passes data between a homepage and a modal page. While it works great on mobile, on a large screen, the modal doesn't fill the whole screen. So the user is able to click outside the screen to dismiss the modal which doesn't trigger any of my functions that that are supposed to trigger on modal dismiss. My question is, how do a disable clicking outside the modal. I don't want the modal to dismiss on click outside, but only when my "close" button is clicked. My modal is set up as:

On HomePage:

open(){
    let modal = this.modalCtrl.create(ModalPage,
        {
            firstName: this.user.firstName,
            lastName: this.user.lastName,
            location: this.user.location
        });
    modal.onDidDismiss(data => {
            this.user.firstName = data.firstName;
            this.user.lastName = data.lastName;
            this.user.location = data.location;
    });
    modal.present();
}

On ModalPage:

closeModal() {
    let data = {
        firstName: this.user.firstName,
        lastName: this.user.lastName,
        location: this.user.location
    }
    this.viewCtrl.dismiss(data);
}

I feel like this should be something very simple, but I can't find any resources online, and the Ionic 2 Doc isn't very clear. Please help.

Make use of the enableBackdropDismiss -option when creating the modal ( link to docs ).

let modal = this.modalCtrl.create(ModalPage, { data: data }, { enableBackdropDismiss: false });

ionic 4

const modal = await this.modalCtrl.create({
  component: ModalPage,
  componentProps: {
    'data': this.data 
  },
  backdropDismiss:false
});

For ionic 4

backdropDismiss:false,

the model should be created like this

 const modal = await this.modalCtrl.create({
      component: SetaddresComponent,
      cssClass: 'my-custom-modal-css',
      componentProps: { },
      showBackdrop:true,
      backdropDismiss:false,
    },

My problem is solved using below code in the Ionic 6

<ion-modal [isOpen]="true" [swipeToClose]="true" [backdropDismiss]="false" [breakpoints]="[0.1, 0.3, 1]"  [initialBreakpoint]="0.3">

This is main keyword for that

[backdropDismiss]="false"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM