Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 Angular - How to self dismiss a modal

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.

like image 414
Eliya Cohen Avatar asked Sep 23 '18 10:09

Eliya Cohen


People also ask

How do you dismiss modals in Ionic 4?

After the modal has been presented, from within the component instance The modal can later be closed or "dismissed" by using the ViewController's dismiss method. Additionally, you can dismiss any overlay by using pop on the root nav controller.


2 Answers

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.

like image 139
Eliya Cohen Avatar answered Nov 15 '22 23:11

Eliya Cohen


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.

like image 42
ciekawy Avatar answered Nov 16 '22 01:11

ciekawy