简体   繁体   中英

How to close a modal window with Nativescript in Angular?

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.

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();
}

I had a similar issue, what I ended up doing was implementing an interface to activate the closeCallback on the modal parameters from the other class. Here is a playground showing what I did https://play.nativescript.org/?template=play-ng&id=q1mDZI&v=6

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