ngx-bootstrap modal:如何从模态中获取返回值?

Fra*_*rzi 28 modal-dialog confirm-dialog ngx-bootstrap angular

在我的Angular 4应用程序中,我们假设我在服务中.

在某些时候,我想要求用户进行确认,目前我只是在做一个confirm(...)请求:

const result = confirm('Are you sure?');

如果相反我想显示一个ngx-bootstrap 模态,比方说,两个按钮"是"或"否",并获得类似的结果?


编辑:在我的情况下,我通过玩主题解决了我的问题.在这里,您可以找到我的解决方案,以防它对其他人有用.然而,该解决方案并没有解决这个问题,即从模态返回一个值,所以我把它打开.

Cha*_*dru 84

试试这样:

myexample它工作正常.希望对你有帮助

home.module.ts

import { ModalModule } from 'ngx-bootstrap';

@NgModule({
    imports: [
        ModalModule.forRoot()
    ]
})
Run Code Online (Sandbox Code Playgroud)

home.component.html

<button class="btn btn-primary" (click)="openConfirmDialog()">Open Confirm box</button>
Run Code Online (Sandbox Code Playgroud)

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);
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

家庭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>
Run Code Online (Sandbox Code Playgroud)

家庭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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我需要将其替换为模态:```canDeactivate() { return inform( 'are you certain?' ); } }``` (2认同)

Chr*_*row 7

我使用了@Chandru的解决方案,但是返回truefalse,而不是:

openConfirmDialog() {
    this.modalRef = this.modalService.show(HomeModalComponent);
    this.modalRef.content.onClose.subscribe(result => {
        console.log('results', result);
    })
}
Run Code Online (Sandbox Code Playgroud)

我只是用了:

openConfirmDialog() {
    this.modalRef = this.modalService.show(HomeModalComponent);
    return this.modalRef.content.onClose;
}
Run Code Online (Sandbox Code Playgroud)