将EventEmitter从子组件发送到ngBootstrap中的父组件

时间:2019-05-16 10:28:51

标签: angular angular7 ng-bootstrap

我想将模态事件从子组件传递到模态的父组件,但出现错误“ modalRf.componentInstance.confirmationEvent不是函数”

子组件:ModalTestComponent.ts

@Input() idModal: string;

@Input() titre: string;

@Input() message: string;

@Output() confirmationEvent = new EventEmitter<any>();

constructor(public activeModal: NgbActiveModal) {
}

public confirmer() {
   this.confirmationEvent.emit();
}

父组件

openActiviteModal() {
   const modalRf = this.modalService.open(ModalTestComponent, 'TestModalId');
   modalRf.componentInstance.titre = 'HELLO WORLD';
   modalRf.componentInstance.message = 'THANKS';
   modalRf.componentInstance.confirmationEvent().subscribe(
       res => {
           if (res && res !== '' && res !== undefined) {
               this.afterConfirmChangementActivite();
           }
       }
   );
}

1 个答案:

答案 0 :(得分:0)

尽管最好在选择器中添加confirmationEvent(请参阅this answer),但我认为您可以通过在modalRf.componentInstance.confirmationEvent之后删除括号来简单地修复代码

modalRf.componentInstance.confirmationEvent.subscribe(
   res => {
       if (res && res !== '' && res !== undefined) {
           this.afterConfirmChangementActivite();
       }
   }
);