Angular 2如何从嵌套组件中冒出一个事件

时间:2016-05-10 20:40:02

标签: angular

我有:

<modal
  #forgotPasswordModal
  [viewModel]="forgotPasswordModalVm"
  (cancelled)="onForgotPasswordModalCancelled($event)"
  (saved)="onForgotPasswordModalSaved($event)">
  <forgot-password></forgot-password>
</modal>

忘记密码表格完成后,我需要通过调用上面的父模态组件上的close()来关闭模态。我可以使用EventEmitter触发事件,但是如何在父容器上接收它?我不希望模态组件中有任何忘记密码特定的日志,也不想使用通用事件服务。

1 个答案:

答案 0 :(得分:1)

<modal
  #forgotPasswordModal
  [viewModel]="forgotPasswordModalVm"
  (cancelled)="onForgotPasswordModalCancelled($event)"
  (saved)="onForgotPasswordModalSaved($event)">
  <forgot-password (close)="onModalClose()"></forgot-password>
</modal>
export class ForgotPasswordComponent {
  @Output() close:EventEmitter = new EventEmitter();

  // execute when the modal should be closed
  onComplete() {
    this.close.emit(null);
  }
}