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

前端 未结 9 1398
一向
一向 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:12

    Try this:

    home.component.ts

    import { BsModalService } from 'ngx-bootstrap/modal';
    import { BsModalRef } from 'ngx-bootstrap/modal';
    
    export class HomeComponent {
     public modalRef: BsModalRef;
     constructor(
        private modalService: BsModalService
     ) { }
    
     openConfirmDialog() {
        this.modalRef = this.modalService.show(HomeModalComponent);
    
        this.modalRef.content.onClose = new Subject<boolean>();
    
        this.modalRef.content.onClose.subscribe(result => {
            console.log('results', result);
         })
     }
    }
    

    and

    home-modal.component.ts

    import { BsModalRef } from 'ngx-bootstrap/modal';
    export class HomeModalComponent {
    
     constructor(private bsModalRef: BsModalRef) {
    
     }
    
     public ngOnInit(): void {
     }
    
     public onConfirm(): void {
        this.bsModalRef.content.onClose.next(true);
        this.bsModalRef.hide();
     }
    
     public onCancel(): void {
        this.bsModalRef.content.onClose.next(false);
        this.bsModalRef.hide();
     }
    }
    
    0 讨论(0)
  • 2020-12-13 03:19

    Try like this :

    myexample it's working correctly. hope this will help you

    home.module.ts

    import { ModalModule } from 'ngx-bootstrap';
    
    @NgModule({
        imports: [
            ModalModule.forRoot()
        ]
    })
    

    home.component.html

    <button class="btn btn-primary" (click)="openConfirmDialog()">Open Confirm box
    </button>
    

    home.component.ts

    import { BsModalService } from 'ngx-bootstrap/modal';
    import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
    
    export class HomeComponent {
        public modalRef: BsModalRef;
        constructor(
            private homeService: HomeService,
            private modalService: BsModalService
        ) { }
    
        openConfirmDialog() {
            this.modalRef = this.modalService.show(HomeModalComponent);
            this.modalRef.content.onClose.subscribe(result => {
                console.log('results', result);
            })
        }
    }
    

    home-modal.component.html

    <div class="alert-box">
        <div class="modal-header">
            <h4 class="modal-title">Confirm</h4>
            <button type="button" class="close" aria-label="Close" (click)="bsModalRef.hide()">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            Are you sure want to delete this node?
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" (click)="onConfirm()">Yes</button>
            <button type="button" class="btn btn-secondary" (click)="onCancel()">No</button>        
        </div>
    </div>
    

    home-modal.component.ts

    import { Subject } from 'rxjs/Subject';
    import { BsModalRef } from 'ngx-bootstrap/modal';
    
    export class HomeModalComponent {
        public onClose: Subject<boolean>;
    
        constructor(private _bsModalRef: BsModalRef) { }
    
        public ngOnInit(): void {
            this.onClose = new Subject();
        }
    
        public onConfirm(): void {
            this.onClose.next(true);
            this._bsModalRef.hide();
        }
    
        public onCancel(): void {
            this.onClose.next(false);
            this._bsModalRef.hide();
        }
    }
    
    0 讨论(0)
  • I do not know if that suits you, but I used a service with Subject. In parent component it is enough to subscribe and unsubscribe, in modal component, when user clicks confirm button, then just emit value using next.

    I'm sorry for not presenting a full example, but pseudocode below should present the idea.

    Service:

    export class SomeService{
        yourSubject: new Subject<string>();
    }
    

    Parent component:

    ...
    ngOnInit(){
        this.bsModalRef = this.someService.yourSubject.subscribe ( val => ...);
    }
    ngOnDestroy(){
        this.bsModalRef.unsubscribe();
    }
    ...
    

    Modal component:

    ...
    onSaveAction(){
        this.someService.yourSubject.next(val);
    }
    ...
    

    Ps. of course you need to provide service in proper place and inject it into components :-)

    0 讨论(0)
提交回复
热议问题