我具有以下代码,可以在单击按钮时打开模式。如何从组件ts文件中的函数打开模态?我的html是这样的:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open The Modal Box</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Sample Modal</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>Click OK to close.</p>
<input [(ngModel)]="name"><br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
</div>
</div>
</div>
</div>
</div>
弹出窗口应来自组件中的以下组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
OpenModalFun() {
//Code to open modal pop-up
}
closeModalFun() {
//Code to close modal pop-up
}
}
我不知道如何从TS文件打开弹出窗口。请帮忙。预先感谢。
答案 0 :(得分:2)
您可以像下面这样隐藏按钮[hidden]="true"
:
<button id="openModalButton" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" [hidden]="true">Open The Modal Box</button>
并显示功能的模态(模拟按钮单击):
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
OpenModalFun() {
document.getElementById("openModalButton").click();
}
closeModalFun() {
//Code to close modal pop-up
}
}
ElementRef
<button #btn id="openModalButton" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" [hidden]="true">Open The Modal Box</button>
并显示功能的模态(模拟按钮单击):
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
@ViewChild('btn') fileInput:ElementRef;
OpenModalFun() {
this.btn.nativeElement.click();
}
closeModalFun() {
//Code to close modal pop-up
}
}
答案 1 :(得分:1)
如果您使用的是ngbootstrap,则可以从TS文件轻松完成
ngbootstrap模态弹出示例