ngx-bootstrap modal: How to get a return value from a modal?

前端 未结 9 1511
一向
一向 2020-12-13 02:23

In my Angular 4 app, let\'s assume that I\'m inside a service.

At some point, I want to ask the user for a confirmation, currently I\'m doing it with just a co

9条回答
  •  遥遥无期
    2020-12-13 03:01

    @ShinDarth You can add this function in your service and call this funcion whenever required.

    In your Service, create this function

        openConfirmDialogBox() {
            this.modalRef = this.modalService.show(DemoModalComponent);
            this.modalRef.content.action.take(1)
                .subscribe((value) => {
                    console.log(value) // here value passed on clicking ok will be printed in console. Here true will be printed if OK is clicked
                    return value;
                 }, (err) => {
                     return false;
            });
        }
    

    In your demo-modal.component.ts, create an EventEmitter

     @Output() action = new EventEmitter();
     public onClickOK() {
        this.action.emit(true); //Can send your required data here instead of true
     }
     public onClickCANCEL() {
        this.action.emit(false); //Can send your required data here instead of true
     }
    

    I hope this would help you

提交回复
热议问题