简体   繁体   English

我试图从component.ts文件中关闭html boostrap模式

[英]I am trying to close html boostrap modal from component.ts file

I have been trying to close a bootstrap modal from a component.ts file after successful submission of the form. 在成功提交表单后,我一直试图从component.ts文件中关闭一个bootstrap模式。

I tried to follow the instructions here: 我试着按照这里的说明操作:

https://ng-bootstrap.github.io/#/components/modal/api which has a stackblitz https://stackblitz.com/run?file=index.html https://ng-bootstrap.github.io/#/components/modal/api有一个stackblitz https://stackblitz.com/run?file=index.html

While the stackblitz has got the open function defined : 虽然stackblitz已经定义了open函数:

open(content) {
    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

While all i want to do is, as shown in the html file 虽然我想做的就是,如html文件中所示

<button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>

More precisely just this: 更确切地说就是这样:

(click)="modal.close('Save click')" 

However, I already have a click-function on the button, which means the same function should be able to call the function to close the modal on successful form submission. 但是,我已经在按钮上有一个单击功能,这意味着相同的功能应该能够在成功提交表单时调用该函数来关闭模式。

I think the part that I am stuck with is this: "A reference to an active (currently opened) modal. Instances of this class can be injected into components passed as modal content." 我认为我所坚持的部分是:“对活动(当前打开)模态的引用。此类的实例可以注入作为模态内容传递的组件中。”

You need to inject the NgbActiveModal object into the component used as the popup dialog, and then call the close method on that reference. 您需要将NgbActiveModal对象注入用作弹出对话框的组件,然后在该引用上调用close方法。

@Component({..})
export class MyDialogComponent {
    public constructor(private _modal: NgbActiveModal) { }

    public close() {
        this._modal.close("result");
    }
}

See this doc reference: 请参阅此文档参考:

https://ng-bootstrap.github.io/#/components/modal/api#NgbActiveModal https://ng-bootstrap.github.io/#/components/modal/api#NgbActiveModal

I would recommend you to use NgbActiveModal class. 我建议你使用NgbActiveModal类。 Feel free to check out the API here . 请在此处查看API。

On your component.ts, you will need to import the NgbActiveModal class. 在component.ts上,您需要导入NgbActiveModal类。

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

constructor(activeModal: NgbActiveModal) {}

close() {
  this.activeModal.close();
  // or this.activeModal.dismiss() depending on your requirements
}

And on your component.html, you may bind the close() method with your click event on your close button. 在component.html上,您可以将close()方法与关闭按钮上的click事件绑定。

<button type="button" class="close" aria-label="Close" (click)="close()">
  <span aria-hidden="true">&times;</span>
</button>

The NgBootstrap site has a decent demo using the NgbActiveModal . NgBootstrap网站使用NgbActiveModal了不错的演示。 I have forked a demo for you reference. 我已经分发了一个演示供您参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据如何从 html 文件传输到 component.ts 文件 - How data is transferred from html file to component.ts file Angular 6 - 将组件显示为来自 component.ts 的模式对话框 - Angular 6 - display component as modal dialog from a component.ts 如何从另一个 Component.ts 访问一个 Component.ts(在同一个 app.component.html 中) - How to access a Component.ts from another Component.ts (in the same app.component.html) 我需要关闭angular4中component.ts中的弹出消息 - I need to close the popup message in component.ts in angular4 加载与component.ts文件不同的component.html文件? - Loading a different component.html file from a component.ts file? 将输入从component.html发送到component.ts - Send input from component.html to component.ts 试图将服务中的数据导入component.ts - Trying to get data from service into component.ts 如何将index.html页面中的值传递给angular4中的component.ts文件 - How to pass a value from index.html page to component.ts file in angular 4 我需要从我的 component.ts 文件中保存的 JSON 对象中获取数据 - I need to fetch data from a JSON object saved in my component.ts file Angular - 当它们在 component.ts 文件中启动时,如何从指令内部检测表单事件? - Angular - How do I detect form events from inside a directive when they are initiated in component.ts file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM