typescript 以编程方式打开 ng-bootstrap 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45133209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open ng-bootstrap modal programmatically
提问by Christian Riese
I've got an angular 4 page including a ng-bootstrap modal. My code looks like this.
我有一个 angular 4 页面,包括一个 ng-bootstrap 模式。我的代码看起来像这样。
foo.html
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.
现在,当我单击按钮时,模态会打开。我现在想做的是在 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!
我的问题是:如何在此处获取“内容”?有没有办法以编程方式获取 ng-template ?提前致谢!
回答by Vega
To access the content
element inside the class, declare:
要访问content
类中的元素,请声明:
@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:
最后在变更检测器中为该元素调用 open 方法:
ngOnChanges(changes: any) {
this.open(this.content);
}