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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:43:19  来源:igfitidea点击:

Open ng-bootstrap modal programmatically

angulartypescriptmodal-dialogng-bootstrapng-template

提问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 contentelement 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);
}