问题描述
我正在使用引导程序模态,但是当我将其从代码隐藏中隐藏时,它会显示以下错误.
I am using bootstrap modal but when I hide it from code-behind it shows the error below.
我正在使用angular6.
I am using angular6.
代码
<div #exampleModal class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
@ViewChild('exampleModal') public exampleModal: ElementRef;
this.exampleModal.hide();
错误消息
推荐答案
您可以在模式对话框中添加关闭按钮HTML,并添加模板变量#closebutton
You can add close button HTML in your modal dialog and add template variable #closebutton
在ts代码中,您调用this.closebutton.nativeElement.click();
关闭模式对话框.
In ts code, you call this.closebutton.nativeElement.click();
to close modal dialog.
HTML
<div class="container">
<h2>Modal Close</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" #closebutton class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Content here</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" id="closeModal" (click)="onSave()">Save</button>
</div>
</div>
</div>
</div>
</div>
TS
export class AppComponent implements OnInit {
@ViewChild('closebutton') closebutton;
constructor(private fb: FormBuilder) { }
public ngOnInit() {
}
public onSave() {
this.closebutton.nativeElement.click();
}
}
演示 https://stackblitz.com/edit/angular-close-bootstrap-modal?file=src/app/app.component.html
更新:
我还有另一种解决方案,没有关闭按钮的窍门.
I have another solution without trick on close button.
第一步,需要通过npm
命令安装jquery
和bootstrap
.
First step you need install jquery
and bootstrap
by npm
command.
第二,您需要在组件中添加declare var $ : any;
Second you need add declare var $ : any;
in component
并且可以使用$('#myModal').modal('hide'); onSave()方法上
And use can use $('#myModal').modal('hide'); on onSave() method
演示 https://stackblitz.com/edit/angular-model-bootstrap-close?file=src/app/app.component.ts
这篇关于如何在角中隐藏引导程序模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!