Angular 角度-如何破坏动态组件?

Angular 角度-如何破坏动态组件?,angular,Angular,我有一个带有此html模板的模态组件: <div class="modal"> <div class="modal-body"> <ng-content></ng-content> </div> <div class="modal-footer"> <button (click)="sendMessage()"

我有一个带有此html模板的模态组件:

<div class="modal">
  <div class="modal-body">
    <ng-content></ng-content>
  </div>

  <div class="modal-footer">
    <button (click)="sendMessage()">success</button>
    <button (click)="close()">abort</button>
  </div>
</div>
在恩格德斯特罗我做到了

this.mySubscription.unsubscribe()

但是在关闭按钮上什么也没发生,我继续从刚刚关闭的模式订阅。我怎样才能解决这个问题?

我想,我有一个办法来解决你的问题。要销毁contentComponent,只需编写
contentComponent.destroy()。然而,问题是您的ModalService如何获得Modal已关闭的信息。为此,您可以使用EventEmitter,如
@Output()closeModal:EventEmitter
请检查代码并告诉我。

我已在答案部分发布了答案。请检查并让我知道。我可以在您的ModalService的open函数中看到您在this.dialogComponentRef中分配了modalComponent,如
this.dialogComponentRef=modalComponent这个.dialogComponentRef.destroy()。我想这可能有用我试过使用这个.dialogComponentRef.destroy(),但它不起作用…好的,那么请检查stackblitz的链接
this.mySubscription = this.modalService.getMessage().subscribe( () => {
  doing things;
)};
this.mySubscription.unsubscribe()