title: angular 5 - how to remove dynamically added components from the dom
body: <p>I'm a beginner in Angular 5, and I'm having the following problem in my project: <br><br>I have a modal which is always present on the body (hidden when not active), and I'm generating the modal's contents dynamically in the modal component. <br><br>The problem is that after closing the modal, and trying to destroy the content, the content is not removed from the dom, and opening the modal again just appends the content to the modal which now contains several elements of the content.<br><br> So far, I've tried doing it in several ways, including using *ngTemplateOutlet with a template, and also by having a custom directive on the container I want to place the content in, and using viewContainerRef and componentFactoryResolver to create the content component dynamically as can be seen here: <br><a href="https://stackoverflow.com/questions/44939878/dynamically-adding-and-removing-components-in-angular">Dynamically ADDING and REMOVING Components in Angular</a>.<br><br>
  Here is my modal component's template (All of the code here refers to the 2nd method):</p>
  
  <pre><code>&lt;div class="modal-backdrop show" (click)="hideModal()"&gt;&lt;/div&gt;
   &lt;div class="modal-dialog show"&gt;
    &lt;div class="modal-content"&gt;
     &lt;ng-container content&gt;&lt;/ng-container&gt;
     &lt;button type="button" class="close" data-dismiss="modal" 
       (click)="hideModal()"&gt;&amp;times;&lt;/button&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>Here is the content directive, the creation of the component is done here:</p>
  
  <pre><code>@Directive({
    selector: '[content]'
   })
  export class ContentDirective {
    constructor(
      public viewContainerRef: ViewContainerRef,
      private componentFactoryResolver: ComponentFactoryResolver
    ) {}
  
  createModalContent(content: { new (): Component }): ComponentRef&lt;{}&gt; {
    const sample = this.viewContainerRef;
    this.viewContainerRef.clear();
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
    content);
    const componentRef = this.viewContainerRef.createComponent(componentFactory);
    return componentRef;
   } 
  }
  </code></pre>
  
  <p>Here is the modal component ts code, trying to destroy the component is in the hideModal method:
  <br></p>
  
  <pre><code>@Component({
    selector: 'app-modal',
    templateUrl: './modal.component.html',
    styleUrls: ['./modal.component.css'],
    entryComponents: [SuggestSkillComponent]
  })
  
  export class ModalComponent implements OnInit {
  
  newComponent: ComponentRef&lt;{}&gt;;
  @Input() opened: boolean;
  @Input() modalType: string;
  @ViewChild(ContentDirective) content: ContentDirective;
  modalState: string;
  subscription: any;
  
  constructor(private modalService: ModalService) {}
  
  hideModal() {
    this.modalService.closeModal();
    this.newComponent.destroy();
  }
  
  ngOnInit() {
     this.subscription = this.modalService.getModalState()
     .subscribe(([modalState, modalContent]) =&gt; {
      if (modalState === 'shown') {
        this.newComponent = this.content.createModalContent(modalContent);
      }
    });
   }
  }
  </code></pre>
  
  <p>The modal is opened through a different component using a modal service method, which recieves the type of component for the content as parameter:</p>
  
  <pre><code> openModal(content: any) {
    this.modalOpened = true;
    this.modalState = 'shown';
    this.emitter.emit([this.modalState, content]);
   }
  </code></pre>
  
  <p>In all ways I've tried so far, I can't get the component I created for the modal's content destroyed after the user is finished with it. Obviously, I'm missing something...<br> Any help will be greatly appreciated.<br><br>Thanks!</p>