title: Simple ModalService for Angular 7 does not open: No component factory found for
body: <p>I am creating an Angular's 7 ModalService that simply opens a Modal (<a href="https://stackblitz.com/edit/mk-angular-modal-service" rel="nofollow noreferrer">StackBlitz Example</a>).</p>
  
  <p>The Modal content should be a Component passed to the Modal when opening.</p>
  
  <p><strong>Modal</strong></p>
  
  <pre><code>export class Modal {
  
    protected modal: any = null;
  
    close() {
      this.modal.close();
    }
  
  }
  </code></pre>
  
  <p><strong>ModalService</strong></p>
  
  <pre><code>import { ApplicationRef, ComponentFactoryResolver, EmbeddedViewRef, Injectable, Injector } from '@angular/core';
  
  @Injectable({
    providedIn: 'root'
  })
  
  export class ModalService {
  
    private componentRef: any;
    private modalContainer: any;
  
    constructor(
      private componentFactoryResolver: ComponentFactoryResolver,
      private appRef: ApplicationRef,
      private injector: Injector) { }
  
    private createFormModal(component: any): Element {
  
      this.componentRef = this.componentFactoryResolver.resolveComponentFactory(component.component).create(this.injector);
  
      this.componentRef.instance.modal = this;
  
      this.appRef.attachView(this.componentRef.hostView);
  
      return (this.componentRef.hostView as EmbeddedViewRef&lt;any&gt;).rootNodes[0] as HTMLElement;
    }
  
    open(component: any): void {
  
      const alertElement = this.createFormModal(component);
  
      const content = document.createElement('div');
      content.classList.add('modal');
      content.appendChild(alertElement);
  
      this.modalContainer = document.createElement('div');
      this.modalContainer.classList.add('modal');
      this.modalContainer.appendChild(content);
  
      document.body.appendChild(this.modalContainer);
  
    }
  
    close(): void {
      this.appRef.detachView(this.componentRef.hostView);
      this.modalContainer.parentNode.removeChild(this.modalContainer);
      this.componentRef.destroy();
    }
  
  }
  </code></pre>
  
  <p>I am not sure if this is the best option but it is not working ...</p>
  
  <p>When I try to open the modal I get the following error:</p>
  
  <pre><code>Error: No component factory found for HelloComponent. 
         Did you add it to  Did you add it to @NgModule.entryComponents?
  </code></pre>
  
  <p>What am I missing? And can I improve this ModalService code?</p>