单击 Ngb 模态背景时的函数调用。角度2模态

function call on clicking NgbModal backdrop. anuglar 2 modal

我正在尝试创建一个函数(比方说 'randomFunction')调用单击 angular2 中使用的模式的背景(阴影部分)使用 NgbModal

这里是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");
     }
 }

有人可以建议我如何进行此操作或如何在模态的 dismiss()close() 函数中调用此 randomFunction()

他们似乎在 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();
      }
  });
}