我正在尝试使用angular2
点击NgbModal
中使用的模式的背景(阴影部分)来调用函数(让我们说'randomFunction')。
以下是companyNumberComponent.html
:
<company-numbers-list (companyNumberModal)="modalService.open(companyNumberModal);"></company-numbers-list>
<template ngbModalContainer #companyNumberModal let-c="close" let-d="dismiss" id="companyNumberModal">
<div class="modal-body">
<company-number-modal></company-number-modal>
</div>
<div class="modal-footer text-center">
<mi-button [type]="'info'" id="number_flow_close" [raised]="true" aria-label="close" (click)="c('Close click');
">Close</mi-button>
</div>
这是companyNumberComponent.ts
文件:
@Component
.....
export class companyNumberComponent(){
constructor(private modalService: NgbModal){}
public randomFunction(){
console.log("hi");
}
}
有人可以建议我如何继续操作或如何在模式的randomFunction()
或dismiss()
函数中调用此close()
。
答案 0 :(得分:3)
他们在 docs 中看到了你想要的东西,即ModalDismissReasons
:
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
open(content) {
this.modalService.open(content).result.then((result) => {}, (reason) => {
if (reason === ModalDismissReasons.ESC || // if you want to check ESC as well
reason === ModalDismissReasons.BACKDROP_CLICK) {
this.randomFunction();
}
});
}
此处似乎没有包含关闭点击,因此您可以在模板_中的点击事件上调用randomFunction
(click)="c('Close click'); randomFunction()"
或者您可以在组件中执行此操作,但在第一个回调中,如果单击关闭按钮,则会向您抛出字符串'Close click'
(或者您在模板中定义的任何内容)。因此,请修改open
:
open(content) {
this.modalService.open(content).result.then((result) => {
if(result === 'Close click') {
this.randomFunction()
}
}, (reason) => {
if (reason === ModalDismissReasons.ESC ||
reason === ModalDismissReasons.BACKDROP_CLICK) {
this.randomFunction();
}
});
}