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

Ale*_*ler 29 ng-bootstrap angular

我有一个模态:

<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 $0.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>
Run Code Online (Sandbox Code Playgroud)

每当我单击是,我希望它调用一个函数并关闭它自己.
在我的控制器@ViewChild('warningModal') warning;submit(),我有this.warning.close();,而且我有,但this.warning.close is not a function每当我点击是时我都会得到.

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

小智 66

为了阐述pkozlowski.opensource的响应,我实际获得对NgbModalRef类的引用的方法是修改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)}`;
});
Run Code Online (Sandbox Code Playgroud)

然后我可以打电话给:

this.modalReference.close();
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力.哦,不要忘记将define modalReference放在类的顶部:

modalReference: any;
Run Code Online (Sandbox Code Playgroud)

  • 如果像他那样定义`modalReference`,你实际上并不需要导入它 (4认同)
  • 很棒的答案!不要忘记导入NgbModalRef! (3认同)

pko*_*rce 18

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

以下是显示此操作的最小示例:http: //plnkr.co/edit/r7watmQuyaUQ8onY17Z1?p = preview

您可能要么混淆不同的库,要么可能还有更多问题,但很难说更多基于所提供的信息.


Jig*_*try 11

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

首先当我们打开Modal

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

之后在 TS 中关闭使用这个

 this.modalService.dismissAll();
Run Code Online (Sandbox Code Playgroud)


Chr*_*nel 10

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

我跑:

    "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.9",
    with angular 5
Run Code Online (Sandbox Code Playgroud)

我必须在我的app.module.ts中导入

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
Run Code Online (Sandbox Code Playgroud)

当然将其添加到提供者:

providers[ NgbModal]
Run Code Online (Sandbox Code Playgroud)

一旦完成这里是模态组件的代码:

 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();
}
Run Code Online (Sandbox Code Playgroud)

https://ng-bootstrap.github.io应该添加一些关于引用重要性的信息..我有点挣扎了解我需要创建一个访问".close()"方法的引用.谢谢大家,它帮助了很多!


Eri*_*ric 7

打开模态

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

modalReference = null;     

constructor(private modalService: NgbModal) {

   }

openVerticallyCentered(content) {
    this.modalReference = this.modalService.open(content, { centered: true });
  }
Run Code Online (Sandbox Code Playgroud)

使用参考关闭模态,就像 Amit 说的

this.modalReference.close();
Run Code Online (Sandbox Code Playgroud)

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