Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何以编程方式关闭ng引导模式?_Angular_Ng Bootstrap - Fatal编程技术网

Angular 如何以编程方式关闭ng引导模式?

Angular 如何以编程方式关闭ng引导模式?,angular,ng-bootstrap,Angular,Ng Bootstrap,我有一个模态: <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;</

我有一个模态:

<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>

&时代;
警告
您输入的数字将导致负费用。我们将按0.00美元处理。你想继续吗?
不
对
无论何时单击“是”,我都希望它调用函数并关闭自身。
在我的控制器中,我有
@ViewChild('warningModal')警告submit()
中,我有
这个.warning.close()
,但每当单击“是”时,我都会看到
this.warning.close不是一个函数

如何使其按我所希望的方式工作?

如果您正在使用(如您的问题所示),事情非常简单-您可以打开一个模态,然后从模板(如代码中)或通过编程方式(通过对返回的
NgbModalRef
类型的对象调用
close()
方法)将其关闭

下面是一个最简单的例子,说明了这一点:


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

您对
@ViewChild('warningModal')警告所做的是获取您在模式中使用的内容,而不是实际的
NgbModalRef


这取决于你如何打开模态,如果你以编程方式打开它,你应该会收到
NgbModalRef
对象,你可以调用它
。关闭

来解释pkozowski.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)}`;
});
然后我可以打电话:

this.modalReference.close();
这很有魅力。哦,别忘了将define modalReference放在类的顶部:

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();
}
应添加一些关于参考重要性的信息。。我有点费劲去理解我需要创建一个引用来访问“.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();

来源:

您在
中有
let-d
let-c
作为变量,两者都是函数。因此,简单的方法是:将
c
d
作为参数传入submit,如下所示
(单击)=“submit(c)”
(单击)=“submit(d)”
,然后在函数中调用
submit(c){c('close model');}
。希望对你有用

您可以通过下面的方式简单地关闭模式

首先,当我们打开模型时

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
类,并通过调用close或disclose函数来关闭自身

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();
    }

}

是ng引导还是ng2引导?谢谢!我就是这样解决的。我只是忘了回来编辑。顺便说一句,你的项目太棒了。plunker不管用,但你的解决方案对我来说很管用,回答太棒了!别忘了导入NgbModalRef!如果像他那样定义
modalReference
,您实际上不需要导入它,您可以选择将参数传递给close,例如,
this.modalReference.close(“提交”)
我发现这个答案直截了当,可以最快地解决我的解决方案。解决这个问题一个小时后,我发现了这个..多亏了它的帮助。此外,模型引用可以设置为键入:
NgbModalRef
。this.modalService.dismissAll();这个案子对我有用!谢谢你,伙计,如果你真的想关闭所有打开的模态,这很好,但如果你想保持一个打开的模态,就不行了。谢谢!这终于为我做到了。