Angular 2 ng-bootstrap close Modal

ⅰ亾dé卋堺 提交于 2019-11-28 09:12:44
user172902

Answer can be found here. ng-bootstrap website is missing lots of information unfortunately Cannot close ng-bootstrap Modal

Inside the component class

  private modalRef: NgbModalRef;

  // Modal
  open(content) {
    this.modalRef = this.modalService.open(content);
    this.modalRef.result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  onSave() {
    this.modalRef.close();
  }
  1. At your modal component you need to add the line :

    @ViewChild('exampleModal') public exampleModal:ModalDirective;
    
  2. At html modal you need to insert in the div root :

    #exampleModal="bs-modal"
    
  3. At your modal component :

    onSave(){
       this.exampleModal.hide();
    }
    

I followed the doc from ng-bootstrap with Angular 6. Finally I found the solution changing one line from the original example:

modal-options.html

<ng-template #content let-d="dismiss">
  <div class="modal-header">
    <h4 class="modal-title">Modal title</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <p>One fine body&hellip;</p>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-light" (click)="d('Close click')">Close</button>
  </div>
</ng-template>

I changed from this let-modal to this let-d="dismiss" and also this 2 lines:

  • (click)="d('Cross click')"
  • (click)="d('Close click')"

modal-options.ts

constructor(private modalService: NgbModal) {}

openLg(content) {
  this.modalService.open(content, { size: 'lg' });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!