ngx-bootstrap BsModal 从子组件关闭
ngx-bootstrap BsModal closing from child component
我在将数据保存到数据库后有一个场景,我需要关闭 Bs-modal
弹出窗口,我的保存是在子组件中完成的,所以我在子组件中使用 ( )在那里输入并使用它来隐藏弹出窗口但无法在子组件中读取我的模式
HTML 父组件
<div bsModal #lgModal2="bs-modal" class="modal fade" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Edit Product</h4>
<button type="button" class="close pull-right" (click)="lgModal2.hide()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<app-edit-product [productId]="prodId" [modalId]="lgmodal2" #child ></app-edit-product>
</div>
</div>
</div>
</div>
子组件 TS
import { BsModalRef } from 'ngx-bootstrap';
export class EditProductComponent implements OnInit {
@Input() modalId:BsModalRef;
somefunction(){
this.modalId.hide();
}
}
Error:An Unexpected error occured!TypeError: Cannot read property 'hide' of undefined
也试过
@Output() closeModal:EventEmitter<Event> = new EventEmitter();
@Input() onHide:any;
然后
somefunction(){
this.closeModal.emit(this.onHide);
}
任何帮助都将非常感谢!
您需要传递事件而不是 Modal 本身:
<app-edit-product [productId]="prodId" (onHide)="lgModal2.hide()" #child ></app-edit-product>
然后在子组件上处理即可:
@Input()
onHide = new EventEmitter<void>();
doHide() {
this.onHide.emit();
}
HTML 家长:
<div bsModal #lgModal2="bs-modal" class="modal fade" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Edit Product</h4>
<button type="button" class="close pull-right" (click)="hideModal()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<app-edit-product [productId]="prodId" [modalId]="lgmodal2" (saveDone)="hideModal()" #child ></app-edit-product>
</div>
</div>
</div>
</div>
子组件 TS:
export class EditProductComponent implements OnInit {
@Output() saveDone: EventEmitter<any> = new EventEmitter<any>();
somefunction(){
this.saveDone.emit();
}
}
父组件 TS:
import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
export class ParentComponent implements OnInit {
@ViewChild('lgModal2') lgModal2: ModalDirective;
hideModal(){
this.lgModal2.hide();
}
}
希望对您有所帮助。
我在将数据保存到数据库后有一个场景,我需要关闭 Bs-modal
弹出窗口,我的保存是在子组件中完成的,所以我在子组件中使用 ( )在那里输入并使用它来隐藏弹出窗口但无法在子组件中读取我的模式
HTML 父组件
<div bsModal #lgModal2="bs-modal" class="modal fade" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Edit Product</h4>
<button type="button" class="close pull-right" (click)="lgModal2.hide()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<app-edit-product [productId]="prodId" [modalId]="lgmodal2" #child ></app-edit-product>
</div>
</div>
</div>
</div>
子组件 TS
import { BsModalRef } from 'ngx-bootstrap';
export class EditProductComponent implements OnInit {
@Input() modalId:BsModalRef;
somefunction(){
this.modalId.hide();
}
}
Error:An Unexpected error occured!TypeError: Cannot read property 'hide' of undefined
也试过
@Output() closeModal:EventEmitter<Event> = new EventEmitter();
@Input() onHide:any;
然后
somefunction(){
this.closeModal.emit(this.onHide);
}
任何帮助都将非常感谢!
您需要传递事件而不是 Modal 本身:
<app-edit-product [productId]="prodId" (onHide)="lgModal2.hide()" #child ></app-edit-product>
然后在子组件上处理即可:
@Input()
onHide = new EventEmitter<void>();
doHide() {
this.onHide.emit();
}
HTML 家长:
<div bsModal #lgModal2="bs-modal" class="modal fade" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Edit Product</h4>
<button type="button" class="close pull-right" (click)="hideModal()" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<app-edit-product [productId]="prodId" [modalId]="lgmodal2" (saveDone)="hideModal()" #child ></app-edit-product>
</div>
</div>
</div>
</div>
子组件 TS:
export class EditProductComponent implements OnInit {
@Output() saveDone: EventEmitter<any> = new EventEmitter<any>();
somefunction(){
this.saveDone.emit();
}
}
父组件 TS:
import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
export class ParentComponent implements OnInit {
@ViewChild('lgModal2') lgModal2: ModalDirective;
hideModal(){
this.lgModal2.hide();
}
}
希望对您有所帮助。