Open ng-bootstrap modal programmatically

喜夏-厌秋 提交于 2019-12-09 08:30:38

问题


I've got an angular 4 page including a ng-bootstrap modal. My code looks like this.

foo.html

[...]
<button class="btn btn-sm btn-secondary material-icons"
(click)="open(content)">search</button>

<ng-template #content let-c="close" let-d="dismiss">
    content in here
</ng-template>
[...]

foo.ts

[...]
constructor(private modalService: NgbModal) { }
[...]
open(content) {
   let options: NgbModalOptions = {
     size: 'lg'
   };

   this.modalService.open(content, options);
}
[...]

Now when I click the button the modal opens. What I want to do now is open the model on ngChanges.

ngOnChanges(changes: any) {
   open(content)
}

My problem is: How do I get the "content" here? Is there any way to get the ng-template programmatically? Thanks in advance!


回答1:


To access the content element inside the class, declare:

@ViewChild('content') private content;

and add the corresponding import at the top of the class:

import { ViewChild } from '@angular/core';

and finally call open method for that element in change detector:

ngOnChanges(changes: any) {
   this.open(this.content);
}


来源:https://stackoverflow.com/questions/45133209/open-ng-bootstrap-modal-programmatically

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