shlomiassaf / ngx-modialog

Modal / Dialog for Angular
http://shlomiassaf.github.io/ngx-modialog
MIT License
686 stars 242 forks source link

(Question) Is there a way to get data back from the modal window #224

Closed h284611 closed 7 years ago

h284611 commented 8 years ago

I am using the modal dialog to catch the users' input. So I am wondering if angular2-modal supports binding custom buttonOnClick functions to the buttons at the bottom of the modal.

Any information and suggestions are highly appreciated.

Gbacc commented 7 years ago

With some digging into the demo page i found it.

The .open() function returns some sort of promise wich you can use like this :

let mod = this.modal.alert()
            .size('lg')
            .showClose(true)
            .title('A simple Alert style modal window')
            .body('modal content')
            .open();

mod.then((resultPromise) => {
            resultPromise.result.then((result) => {
                this.result = result;
            }, () => this.result = 'Rejected!');
        });

In case you are using custom modals component there is an exemple here : https://github.com/shlomiassaf/angular2-modal/blob/master/src/demo/app/bootstrap-demo/bootstrap-demo-page/custom-modal-sample.ts

You can return your result in the dialog.close() :

this.dialog.close('my result');

Hope it helps.

nikked commented 7 years ago

Dear Gbacc,

I am struggling with this same issue with custom modals.

I am calling my custom modal in a similar fashion as in Shlomi Assaf's example Plunker.

openCustom(){ this.modal.open(CustomModal); } You mentioned you can return a result from the modal in the dialog close:

this.dialog.close('my result');

My question is how can you refer to this returned result from the component that calls the custom modal? How does the calling component know when the custom modal has returned the result?

Thanks in advance! I am sorry if this something obvious, I am very new to Angular 2.

Gbacc commented 7 years ago

Hello again,

I made an exemple here from the exemple Plunker.

Custom modals returns the same result as normal one. this.modal.alert()....open() returns a promise wich you can use with the angular .then() operator.

Not sure if i'm clear.

h284611 commented 7 years ago

Thank you Gbacc, for putting your solution on plunker !

That answers my question.