如何以编程方式关闭 ng-bootstrap 模式?

How to programmatically close ng-bootstrap modal?

我有一个模态:

<template #warningModal let-c="close" let-d="dismiss">
  <div class="modal-header">
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">Warning</h4>
  </div>
  <div class="modal-body">
      The numbers you have entered result in negative expenses.  We will treat this as [=11=].00.  Do you want to continue?
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" (click)="c('Close click')">No</button>
    <button type="button" class="btn btn-secondary" (click)="submit()">Yes</button>
  </div>
</template>

每当我单击“是”时,我希望它调用一个函数并自行关闭。
在我的控制器中,我有 @ViewChild('warningModal') warning;,在 submit() 中,我有 this.warning.close();,但只要我单击是,我就会得到 this.warning.close is not a function

我如何让它按照我想要的方式工作?

如果您正在使用 https://ng-bootstrap.github.io/(如您的问题所示),事情就非常简单 - 您只需打开一个模式,然后从模板中关闭它(如您的代码中所示)或以编程方式(通过对返回的 NgbModalRef 类型的对象调用 close() 方法)。

这里是一个最小的例子,展示了这一点:http://plnkr.co/edit/r7watmQuyaUQ8onY17Z1?p=preview

您可能混淆了不同的库,或者您的问题可能还有更多内容,但仅根据提供的信息很难说得更多。

您对 @ViewChild('warningModal') warning 所做的是获取您在模态中使用的 TemplateRef,而不是实际的 NgbModalRef

这取决于您打开模式的方式,如果您以编程方式打开它,您应该会收到 NgbModalRef 对象,您可以在该对象上调用 .close

为了阐述 pkozlowski.opensource 的回应,我实际获得对 NgbModalRef class 的引用的方式是修改他在 this.modalService.open 上的 plunker 中的内容,如下所示:

this.modalReference = this.modalService.open(content);
this.modalReference.result.then((result) => {
  this.closeResult = `Closed with: ${result}`;
}, (reason) => {
  this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});

然后我就可以打电话了:

this.modalReference.close();

这很有魅力。哦,别忘了把 define modalReference 放在 class:

的顶部
modalReference: any;

与 TBrenner 不同,我不能只使用 modalReference: any;

我运行与:

    "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.9",
    with angular 5

我必须在 app.module.ts

中导入
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

当然还要将其添加到提供商中:

providers[ NgbModal]

完成后就是来自模态组件的代码:

 import { Component, Input } from '@angular/core'; 
 import { ModalDismissReasons, NgbModal, NgbModalRef } from '@ng 
 bootstrap/ng-bootstrap';

export class MyClass {
modalReference: NgbModalRef;

constructor(private modalService: NgbModal)
open(content) {
this.modalReference = this.modalService.open(content);
this.modalReference.result.then((result) => {
  this.closeResult = `Closed with: ${result}`;
}, (reason) => {
  this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
}

private getDismissReason(reason: any): string {
if (reason === ModalDismissReasons.ESC) {
  return 'by pressing ESC';
} else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
  return 'by clicking on a backdrop';
} else {
  return `with: ${reason}`;
}
}

JoinAndClose() {
this.modalReference.close();
}

https://ng-bootstrap.github.io 应该添加一些关于引用重要性的信息。我费了点功夫才明白我需要创建一个引用来访问“.close()”方法。谢谢大家,帮了大忙!

打开模式

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

modalReference = null;     

constructor(private modalService: NgbModal) {

   }

openVerticallyCentered(content) {
    this.modalReference = this.modalService.open(content, { centered: true });
  }

使用引用关闭模态,如 Amit 所说

this.modalReference.close();

来源:https://ng-bootstrap.github.io/#/components/modal/examples

您在 <template #warningModal let-c="close" let-d="dismiss"> 中有 let-dlet-c 作为变量,它们都是函数。所以,简单的方法是:将 cd 作为参数传递到提交中,像这样 (click)="submit(c)"(click)="submit(d)" 然后在函数中调用 submit(c){ c('close modal'); }.希望对你有用。

您可以通过以下方式简单地关闭模态。

首先打开Modal

this.modalService.open(content, { size: "lg" }).result.then(
      result => {
        this.closeResult = `Closed with: ${result}`;
      },
      reason => {
        this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
      }

之后在 TS 中关闭使用这个

 this.modalService.dismissAll();

在我看来:模态负责关闭自身而不是模态的调用者。

模态框可以简单地通过注入器访问 NgbActiveModal class,并通过调用关闭或关闭函数关闭自身。

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

export class Modal {

    constructor(private activeModal: NgbActiveModal) {
    }

    public submit() {
        this.activeModal.close(/* your result */);
    }

    public close() {
        this.activeModal.close();
    }

}

有一种从集中式应用程序组件级别执行此操作的 hacky 方法

this._platformLocation.onPopState(() => {
        document.getElementsByClassName('modal-header')[0].getElementsByTagName('a')[0].click();
    });

这应该放在 app.component.ts 文件中的构造函数中。