ankosoftware / ng2-bootstrap-modal

Library to simplify the work with bootstrap modal dialogs
MIT License
54 stars 62 forks source link

DialogService.addDialog().subscribe() not working? #64

Open miliu99 opened 6 years ago

miliu99 commented 6 years ago

I want to run a callback function when modal dialog is missed. It used to work fine, but today I suddenly realize no more and I'm not sure since when. The modal dialog itself still works without problem. So, why is subscribe() part not working?

I'm using ng2-bootstrap-modal (only for modal) together with ngx-bootstrap (for everything else) if it matters. Also, I'm using bootstrap 3.3.7 and angular 4.4.3.

    private showConfirm(options, cb?) {
        this.dialogSvc.addDialog(ModalConfirmComponent, options).subscribe((isConfirmed) => { if (isConfirmed && cb) cb(); });
    }
miliu99 commented 6 years ago

After some digging, I figured out why my subscribe() function is not called. In dialog.component.ts, callback is supposedly called within ngOnDestroy, but it never hit it, although my dialog did call ngOnDestroy.

    DialogComponent.prototype.ngOnDestroy = function () {
        if (this.observer) {
            this.observer.next(this.result);
        }
    };

I'm not sure what's the right fix, but for now, if I move that call to close(), it seems to work fine.

 DialogComponent.prototype.close = function () {
        if (this.observer) {
            this.observer.next(this.result);
        }
        this.dialogService.removeDialog(this);
    }; 

Hope somebody will point me a right fix.

vpnocsen commented 6 years ago

I got this problem before when i implement OnDestroy in the my component (in your case 'ModalConfirmComponent') -> it will overwrite the OnDestroy in DialogComponent. Just remove it and write a custom close dialog method to wrap all the code in your destroy method.

for ex:

  closeDialog() {
           this.searchSubscription && this.searchSubscription.unsubscribe();
           this.close();
    }
miliu99 commented 6 years ago

Once I got your explanation, I realize that I can fix it by calling super.ngOnDestroy() in my ngOnDestroy, and it works. Thanks!