Javascript 在单击事件外部访问ngx引导模式
我正在从我的应用程序组件中的ng模板中调用。我已在中构建了当前设置 正如你所看到的,我已经在modals取消/关闭按钮中添加了另一个事件-当单击这些按钮时,会触发两个事件。一个用于关闭模态,另一个用于执行“其他操作” 我希望能够将这个“其他东西”应用到在模式外单击时触发的“背景”事件。默认情况下,单击背景时模式关闭(Javascript 在单击事件外部访问ngx引导模式,javascript,angular,bootstrap-modal,ngx-bootstrap,Javascript,Angular,Bootstrap Modal,Ngx Bootstrap,我正在从我的应用程序组件中的ng模板中调用。我已在中构建了当前设置 正如你所看到的,我已经在modals取消/关闭按钮中添加了另一个事件-当单击这些按钮时,会触发两个事件。一个用于关闭模态,另一个用于执行“其他操作” 我希望能够将这个“其他东西”应用到在模式外单击时触发的“背景”事件。默认情况下,单击背景时模式关闭(this.modalRef.hide();)。有一个配置可以删除关闭行为(ignoreBackdropClick),但这不是我想要的。我希望能够保持默认行为,并添加另一个函数,就像模
this.modalRef.hide();
)。有一个配置可以删除关闭行为(ignoreBackdropClick
),但这不是我想要的。我希望能够保持默认行为,并添加另一个函数,就像模态上的其他触发器一样
因此,我需要能够“访问”背景事件,以便对其应用函数-没有html句柄可以执行此操作
component.html
<ng-template #printTemplate>
<div class="modal-header">
<h4>Title</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="cancel(); anotherFunc()">
<span aria-hidden="true" class="red">×</span>
</button>
</div>
<div class="modal-body">
<app-some></app-some>
</div>
<div class="modal-footer d-block">
<button class="btn btn-secondary float-right mr-4" (click)="cancel(); anotherFunc()">Cancel</button>
</div>
</ng-template>
标题
&时代;
取消
组件。ts
openModal(printTemplate: TemplateRef<any>) {
this.modalRef = this.modalService.show(printTemplate);
}
cancel() {
this.modalRef.hide();
}
anotherFunc() {
console.log("func triggered");
}
openmodel(打印模板:TemplateRef){
this.modalRef=this.modalService.show(打印模板);
}
取消{
this.modalRef.hide();
}
另一个函数(){
console.log(“func触发”);
}
要实现您的要求,您可以在显示modal并单击Background后阅读事件。这是一张工作票
config={
背景:没错,
ignoreBackdropClick:false
};
OpenModel(打印模板:TemplateRef){
this.modalRef=this.modalService.show(打印模板,this.config);
this.modalRef.onHide.subscribe((原因:string | any)=>{
如果(原因=='背景点击'){
this.myFunc();//在此处运行函数
}
});
};
注意:在ngx bootstrap 6.1.0上进行了测试,打字在以下版本中似乎已中断,但可能可以通过更改打字来修复。您好!6.1.0版是有可能的,在你的stackblitz中它被设置为3.0.0版。你想要哪个解决方案?(为了使3.0.0按预期工作,似乎出现了一些类型问题)谢谢。我实际上是在3.3.0上工作,所以是的,确实有问题。@TomRudge仔细检查了过去的情况,似乎仅仅改变打字是不够的
config = {
backdrop: true,
ignoreBackdropClick: false
};
openModal(printTemplate: TemplateRef<any>) {
this.modalRef = this.modalService.show(printTemplate, this.config);
this.modalRef.onHide.subscribe((reason: string | any) => {
if(reason === 'backdrop-click') {
this.myFunc(); // run your function here
}
});
};