单击Ngb模态背景的函数调用.Anuglar 2 modal



我正在尝试使用NgbModal单击angular2中使用的模态的背景(阴影部分)时调用一个函数(假设"随机函数")。

这是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()

似乎他们在文档中有你要寻找的东西,即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();
      }
  });
}

此处似乎根本不包含关闭点击,因此您可以在template_中调用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();
      }
  });
}

最新更新