Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modal): pass custom data to the hide() method in Modal #2330

Open
corn-code opened this issue Aug 1, 2017 · 15 comments
Open

feat(modal): pass custom data to the hide() method in Modal #2330

corn-code opened this issue Aug 1, 2017 · 15 comments

Comments

@corn-code
Copy link

Could you please explain how to pass custom data to the hide() method
this.dynamicModal.hide(data);

@IlyaSurmay
Copy link
Contributor

There's no such possibility for now. Can you explain why do you need it?

@corn-code
Copy link
Author

corn-code commented Aug 1, 2017

Yes. Let's imagine that we have a next snippet of code:

<button (click)="onDelete()">Delete</button>
class Project {
     onDelete() {
       this.dynamicModal.show();
      // I want to get data array here from the method hide()
     }
}

In modal component, I want to transform some data and return this value to the class Project

class MyModalComponent {
     public data = [];
     first() {
       this.dynamicModal.hide(data.filter(x => isPositive(x)));
     }
}

Does it clear?

@adrianolsk
Copy link

adrianolsk commented Aug 9, 2017

I also need this.

I do some work in the modal and when it closes I need to refresh the data in the component that called the modal. But I need to know when the user closed or finish the task and are done.
Would be nice to have another method that close with parameters
modalRef.close(somedata);

But I just solved the problem passing a function to the modal. and Call it with parameters when I am done, and closing the modal in the parent component @CornCodeCorp check it out

this.modalRef = this.modalService.show(MyModalComponent);
this.modalRef.content.onClose = (myData) => {
      // Do something with myData and then hide
      this.modalRef.hide();
};

and in the modal:

onClose: any;

close(){
      this.onClose({ value: 100})
}

@ctrl-brk
Copy link

ctrl-brk commented Aug 9, 2017

You can declare your modal in the same template as your component and share everything.

@corn-code
Copy link
Author

My idea is to create one general ModalComponent in the root of application and call this component via ModalService in any places. Also, ModalService accepts one of my components for loading dynamically in the ModalComponent.

@faizalshap
Copy link

You can set DismissReason before closing the popup.
this._modalService.setDismissReason('Yes');

And then,
this._modalService.onHide.subscribe((result) => { if (result === 'Yes') { //do something... } }

@corn-code
Copy link
Author

@faiz2rock Thank you. I found the same solution.
this._modalService.dismissReason = JSON.stringify({x: 1, y: 2})

@NenadJovicic
Copy link

NenadJovicic commented Sep 19, 2017

+++ for this
You should make something like Angular Material 2 Dialog
And to make bsModalRef for every new modal created as component
so you can subscribe on closing specific modal that is created as component

@biosis21
Copy link

Buy the way, you can do the same flow as in native <dialog></dialog> element and API provided with him. https://www.instagram.com/p/BYl-j_RjFAs/?taken-by=corn.code

@kahboom
Copy link

kahboom commented Oct 16, 2017

I need this for the show() method for popovers as well, similarly to how ng-bootstrap allows. An example is to be able to pass data more easily when developing plugins for third-party libraries that want to use ngx-bootstrap.

@piernik
Copy link

piernik commented Mar 22, 2018

I need that option too. Best solution is this.modalRef.hide(myValue); and then

   this.modalService.onHide
      .pipe(
        take(1)
      )
      .subscribe(resp => {
        if (resp !== null) {
        }
      });

reason should be moved somewhere else in my opinion

@WuglyakBolgoink
Copy link

WuglyakBolgoink commented Jan 14, 2019

How can I return data from modal if I use only component-based show() in Service?

Update:
Ok... With some trick we can pass data back.

From modal component in close() just add modalService.setDismissReason(any) before hide():

this.bsModalService.setDismissReason(JSON.stringify{
            mode: 'closed',
            data: [ 1, 2, 3 ]
}));
this.myModal.hide();

@devel112
Copy link

Also unable to reliably get return value from the actual modal - not enough to generally use the service to listen for any modal to close as there may be many. How do i find out if my specific modal has closed with f. eks ESC?

@JIBIN-P
Copy link

JIBIN-P commented Nov 1, 2019

Why isn't modalService.setDismissReason mentioned anywhere in the modal API documentation? Also, I would like to contribute to the documentation can you tell me where is the documentation located in this repo.

@matheussouza9
Copy link

I solved the problem this way

Service

export class ModalService {
    constructor(private _modalService: BsModalService) { }

    openModal<T>(component: T, config?: ModalOptions): Promise<any> {
        return new Promise((resolve, reject) => {
            const modalRef = this._modalService.show(component, config);
            this._modalService.onHidden.subscribe((reason: string) => {
                resolve(modalRef.content)
            })
        })
    }
}

Modal Component

export class ErrorComponent {
    data = {}

    constructor(public bsModalRef: BsModalRef) { }

    closeModal(): void {
        this.data = { a: 1, b: 2 }
        this.bsModalRef.hide();
    }
}

Component that shows modal and needs receive some data after modal is hidden

this.modalService.openModal(ErrorComponent).then(
    (content: ErrorComponent) => {
        if (content.data) {
          this.reloadData(content.data)
        } 
    }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.