How to instantiate In angular2 ngx-bootstrap modal when the modal is open

柔情痞子 提交于 2019-12-25 12:11:51

问题


This is my parent component template fragment:

<button type="button" (click)="addModal.show();">Add</button>
<add-modal #addModal></add-modal>

And this is my modal component:

Component({
    selector: 'card-add-modal',
    template: `
        <div *ngIf="isShowModal" bsModal #addModal="bs-modal" [config]="{show: true, backdrop: 'static'}" (onHidden)="onHidden()" class="modal fade">
            <!-- header... -->

            <div class="modal-body">
                <input type="text" name="name" id="name" [(ngModel)]="model.test">
            </div>

            <!-- footer... -->
        </div>
    `
})
export class ModalComponent{
    @ViewChild('addModal') public addModal: ModalDirective;
    isShowModal: boolean = true;
    model: any = { test: 'hey modal...' };

    show(){ this.isShowModal = true; }
    hide(){ this.addModal.hide(); }
    onHidden(){ this.isShowModal = false; }
}

Okay, It's working fine now.

But after I click the Add button, Modal show, I typed something in the input box and then closed and reopened, and the contents of those inputs still exist.

I found that this is actually on the page, the modal has been instantiated to complete, and here show and hide just add delete DOM, and modal component will not be destroyed.

However, I hope that every time you click add to re-create a modal instance, after the destruction of the current modal, I did not find this way.

来源:https://stackoverflow.com/questions/43537762/how-to-instantiate-in-angular2-ngx-bootstrap-modal-when-the-modal-is-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!