title: error "ERROR TypeError: Cannot read property 'createComponent' of undefined" when work with dynamic component in Angular2
body: <p>I am trying to add the components dynamically in angular4.
  I checked with other questions, But i cant find solution.
  I got the error </p>
  
  <blockquote>
    <p>ERROR TypeError: Cannot read property 'createComponent' of undefined</p>
  </blockquote>
  
  <p>on dynamic components.</p>
  
  <p><strong>adv.component.ts</strong></p>
  
  <pre><code>import { Component, OnInit, AfterContentInit, ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
  import { SampleComponent } from '../sample/sample.component';
  @Component({
    selector: 'app-adv',
    templateUrl: './adv.component.html',
    styleUrls: ['./adv.component.css']
  })
  export class AdvComponent implements OnInit, AfterContentInit {
    @ViewChild('container', {read:'ViewContainerRef'}) container;
    constructor(private resolver : ComponentFactoryResolver) { }
  
    ngOnInit() {
    }
  
    ngAfterContentInit(){
      const sampleFactory = this.resolver.resolveComponentFactory(SampleComponent);
      this.container.createComponent(sampleFactory);
    }
  
  }
  </code></pre>
  
  <p><strong>adv.component.html</strong></p>
  
  <pre><code>&lt;div #container&gt;&lt;/div&gt;
  </code></pre>
  
  <p><strong>app.module.ts</strong></p>
  
  <pre><code>import { BrowserModule } from '@angular/platform-browser';
  import { NgModule } from '@angular/core';
  
  import { AppComponent } from './app.component';
  import { AdvComponent } from './adv/adv.component';
  import { SampleComponent } from './sample/sample.component';
  
  @NgModule({
    declarations: [
      AppComponent,
      AdvComponent,
      SampleComponent
    ],
    entryComponents:[
      SampleComponent
    ],
    imports: [
      BrowserModule
    ],
    providers: [],
    bootstrap: [AppComponent]
  })
  export class AppModule { }
  </code></pre>