sachman
sachman

Reputation: 403

NgbModal - Return result to parent when opening sub Modal

I am using NgbModal in my component called MyComponent and after I open one modal depending on the user action I might need to open another modal from the first modal. Now I am able to get a value returned when the first modal closes as shown in the example https://medium.com/@izzatnadiri/how-to-pass-data-to-and-receive-from-ng-bootstrap-modals-916f2ad5d66e . Is there a way to do this in the second modal that opens so it returns a result back to MyComponent?

Here is my code the open the first modal from MyComponent:

        let options:NgbModalOptions = {
            size: 'lg',
            backdrop: 'static'
        };

        const modalRef = this.modalService.open(Modal1Component, options);

Now within the Modal1Component component I open a second modal called Modal2Component when a user clicks a button as follows:

        this.closeModal();
        let options:NgbModalOptions = {
            size: 'lg',
            backdrop: 'static'
        };
        const modalRef = this.modalService.open(Modal2Component, options);

As shown in the code above the first modal Modal1Component is closed before opening Modal2Component . Now I want to return a result from Modal2 component back to MyComponent after Modal2 closes. Is it possible?

Upvotes: 0

Views: 2932

Answers (1)

JB Nizet
JB Nizet

Reputation: 691745

You can open the second modal first, get its result promise, and pass that as argument when closing the first modal:

const modalRef = this.modalService.open(Modal2Component, options);
this.activeModal.close(modalRef.result);

Demo

Upvotes: 1

Related Questions