我将数据保存到数据库后有一个场景我需要关闭Bs-modal
弹出窗口,我的保存是在子组件中完成的,所以我使用()输入了子组件中的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();
}
}
错误:发生了意外错误!TypeError:无法读取未定义的属性“hide”
也尝试了
@Output() closeModal:EventEmitter<Event> = new EventEmitter();
@Input() onHide:any;
然后
somefunction(){
this.closeModal.emit(this.onHide);
}
任何帮助都将非常感谢!
答案 0 :(得分:3)
HTML Parent :
<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();
}
}
PARENT COMPONENT 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();
}
}
希望这有帮助。
答案 1 :(得分:1)
您需要传递事件而不是Modal本身:
<app-edit-product [productId]="prodId" (onHide)="lgModal2.hide()" #child ></app-edit-product>
然后在子组件上处理它:
@Input()
onHide = new EventEmitter<void>();
doHide() {
this.onHide.emit();
}