How to close a modal window with Nativescript in Angular?

Viewed 3252

I have a modal using the modal service:

import { ModalDialogService } from "nativescript-angular/directives/dialogs";    
private modal: ModalDialogService,

I can call a modal, for example

  this.modal.showModal(MyModalComponent, options).then(res => {
      // console.log(res);
  }); 

Now I want to close the modal, but not from within the modal itself. This already works in iOS:

        const page = topmost().currentPage;
        if (page && page.modal) {
          page.modal.closeModal();
        } else {
          console.log("error closing modal!!!!");
        }

But running this with Android, it will always goes to the error console.log, resulting the modal not being closed. The user can still close it (the modal has a close button), but I also want to programaticly close the modal.

2 Answers

That's not the way how you should do it in Angular. Inject ModalDialogParams and use the closeCallback method.

constructor(private modalDialogParams: ModalDialogParams) {}

onCloseButtonTap() {
    this.modalDialogParams.closeCallback();
}
Related