23

In Ionic 3, dismissing a modal was pretty simple:

constructor(viewCtrl: ViewController) {
    this.viewCtrl.dismiss()
}

In Ionic 4, I can't import ViewController (or to be accurate, my IDE tries to import a type of ViewController).

I was wondering what the new approach of dismissing a modal is.

Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116

5 Answers5

39

According to the docs, it looks like the dismiss method has moved to ModalController.

So to dismiss a modal, I need to do:

constructor(modalCtrl: ModalController) {
  modalCtrl.dismiss();
}

How i(r)onic that I find my answer AFTER I post the question.

Loz
  • 118
  • 11
Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116
11

the ionic v4 documentation seems to be missing here yet I believe the correct way to access dismiss from the modal is:

import { Components } from '@ionic/core';

@Component({
  selector: 'app-some-modal',
  templateUrl: 'some-modal.component.html',
  styleUrls: ['some-modal.component.scss']
})
export class SomeModalComponent {
  // modal is available here where created with:
  // modalController.create({ component: SomeModalComponent})
  @Input() modal: Components.IonModal;

  onCancel = () =>
    this.modal.dismiss('cancel');
}

while the modal is actually of type HTMLIonModalElement I am using Components.IonModal since HTMLIonModalElement is supposed to be global yet it is not visible to WebStorm/IntelliJ for some reason.

ciekawy
  • 2,238
  • 22
  • 35
  • 2
    Thank you @ciekawy - one day when Google prioritizes v4 over v3 documentation and when there is enough Stack Overflow answers to fill the gaps of missing documentation using Ionic will be so much fun! :D – Grant May 04 '19 at 23:55
  • 1
    Writing from the future, it's still a nightmare because the framework is completely reinvented every 9 months. – John Dec 09 '21 at 20:45
5

To dismiss a modal in ionic v4. Do the following in your ionic modal component

    export class ExampleModalComponent implements OnInit {
      constructor(private navCtrl: NavController, private modalCtrl: ModalController) {

      }

      async closeModal() {
        await this.modalCtrl.dismiss();
      }
    }

In Your Components HTML the button should look something like this.

<ion-button (click)="closeModal()">
  <ion-icon slot="icon-only" name="arrow-back"></ion-icon>
</ion-button>
paulobunga
  • 277
  • 6
  • 13
1

You can use the self dismiss the modal like this.

constructor(
    private modal: ModalController,
) { }

dismiss() {
    this.modal.dismiss();
}
Tommy Wong
  • 89
  • 1
  • 2
0

on ionic 6

the modal can dismiss put (click)="modal.dismiss()":

<ion-modal #modal [keepContentsMounted]="true" (click)="modal.dismiss()">
sadalsuud
  • 460
  • 4
  • 12