【发布时间】:2019-12-13 07:18:37
【问题描述】:
我有一个进入引导模式的表单,我想在用户单击提交按钮时关闭模式。看起来像这样:
export class AddPartModalComponent implements OnInit {
@Input() id:number;
closeResult: string;
constructor(private modalService: NgbModal, private graphService:GraphService) { }
ngOnInit() {
}
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)}`;
});
}
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}`;
}
}
onSubmit(form: NgForm) {
//some code
}
}
<ng-template #content let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">New element</h4>
<button type="button" class="close" aria-label="Close" (click)="modal.dismiss('')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form (ngSubmit)="onSubmit(f);" #f="ngForm"><!--here if I add 'modal.dismiss('Cross click')', it dosen't submit or it dosen't close the modal (depend if I add it before or after the 'onSubmit()')-->
<!--some form content-->
<button class="btn btn-primary" type="submit" [disabled]="f.invalid">Create</button>
</form>
</div>
</ng-template>
<button (click)="open(content)" style="background-color: transparent; height: 100%; width:100%; border: none;"><h4 style="height: 100%; width: 100%;">+ add part +</h4></button>
所以我不知道如何关闭模态,有没有办法在onSubmit函数中关闭它?
我尝试在onSubmit 函数中使用modal.dimiss,但它不起作用,我可能不明白引导模式是如何工作的,如果您有任何建议,我会很乐意阅读。
【问题讨论】:
标签: html angular typescript bootstrap-4