[英]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">×</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.