title: Dynamically load component in div - Angular5
body: <p>I was able to successfully create a button that on click loads another instance of my component into the DOM, but it loads it outside the component I want it to be inside which results with the CSS being off. When I add "editorComponents" to my page it currently looks like this: </p>
  
  <p><a href="https://i.stack.imgur.com/SmUbr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SmUbr.png" alt="enter image description here"></a></p>
  
  <p>But I want it to look like this: </p>
  
  <p><a href="https://i.stack.imgur.com/6PCYp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6PCYp.png" alt="enter image description here"></a></p>
  
  <p>You can see from the inspection that in the second example all of the  tags are manually moved to where I want them to be.</p>
  
  <p>Right now my code is as follows: </p>
  
  <p><strong>home.component.html</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="row backgroundContainer"&gt;
  
    &lt;div class="col-md-7 componentContainer"&gt;
      &lt;app-editor&gt;&lt;/app-editor&gt;
      &lt;button (click)="addEditor()" type="button"&gt;Add Editor&lt;/button&gt;
    &lt;/div&gt;
  
    &lt;div class="col-md-5 componentContainer"&gt;
      &lt;app-bible&gt;&lt;/app-bible&gt;
    &lt;/div&gt;
  
    &lt;div id="footerBox"&gt;
      &lt;app-footer&gt;&lt;/app-footer&gt;
    &lt;/div&gt;
    
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>home.component.ts</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Component, OnInit, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
  import { EditorComponent } from '../editor/editor.component';
  
  @Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
  })
  export class HomeComponent implements OnInit {
  
    constructor(private componentFactoryResolver: ComponentFactoryResolver,
      private viewContainerRef: ViewContainerRef) {}
  
    ngOnInit() {
    }
  
    addEditor() {
      const factory = this.componentFactoryResolver.resolveComponentFactory(EditorComponent);
      const ref = this.viewContainerRef.createComponent(factory);
      ref.changeDetectorRef.detectChanges();
    }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>So how do I make it so when I dynamically add an EditorComponent to my page it is added directly below the current "app-editor" in my html?</p>