简体   繁体   English

ng-bootstrap modal:通过打字稿打开

[英]ng-bootstrap modal: open it via typescript

I'm having problems, I'd like help opening a mode from the component. 我遇到了问题,我想帮助您从组件中打开模式。 I would like to open the modal when the user has been verified in a form. 在表单中验证用户后,我想打开模式。

.component.html .component.html

<ng-template #modalExample let-c="close" let-d="dismiss">
/** Modal body **/
</ng-template>

<form (ngSubmit)="f.form.valid && signin()" #f="ngForm" class="">
/** Buttons, labels, buttons **/ 
</form>

.component.ts .component.ts

signin() { 
/**Calling backend**/ 
this.modalService( *REFERENCE TO MODAL HERE* )  
}

I've tried using 我试过使用

@ViewChild('modalExample') modalExample: ElementRef
this.modalService.open(this.modalExample);

The open method expects either a component or a template ref to function correctly, and ViewChild gives you a TemplateRef by default. open方法期望组件或模板ref正常运行,并且默认情况下,ViewChild为您提供TemplateRef。 To resolve this change your ViewChild declaration to: 要解决此问题,请将您的ViewChild声明更改为:

@ViewChild('modalExample', {read: TemplateRef}) modalExample: TemplateRef<any>;

and pass that through to this.modalService.open(this.modalExample) when you want to open the modal and that should work. 并在想要打开模式时将其传递给this.modalService.open(this.modalExample)

The Bootstrap site has various examples you can look to see how to open the modal. Bootstrap站点上有各种示例,您可以查看它们如何打开模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM