title: Angular 2 dynamically add component instance into container
body: <p>I'm trying to implement in Angular 2 a ComponentContainer so you can dynamically add other Components, similar to, for example, a LinearLayout in Android.</p>
  
  <p>So far, using
  <a href="https://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468">Angular 2 dynamic tabs with user-click chosen components</a>
  I can dynamically add Components passing the Type.</p>
  
  <p>My problem is that using this method, the created Component is created only by its Type and I don't know how to give arguments to it.</p>
  
  <p>This is what i've done:</p>
  
  <p><strong>container.component.ts</strong></p>
  
  <pre><code>import {
      Component,
      OnChanges,
      AfterViewInit,
      OnDestroy,
      Input,
      Type,
      ViewChild,
      ViewContainerRef,
      ComponentRef,
      ComponentResolver,
      ComponentFactory
  } from '@angular/core';
  
  @Component({
      selector: 'wrapper-component',
      template: '&lt;div #container&gt;&lt;/div&gt;'
  })
  class WrapperComponent implements OnChanges, AfterViewInit, OnDestroy {
  
      @Input() type: Type;
      @ViewChild('container', {read: ViewContainerRef}) container;
  
      cmpRef: ComponentRef&lt;any&gt;;
  
      private isViewInitialized: boolean = false;
  
      constructor(private resolver: ComponentResolver) { }
  
      private updateComponent() { 
          if(!this.isViewInitialized)
              return;
  
          if(this.cmpRef)
              this.cmpRef.destroy();
  
          this.resolver.resolveComponent(this.type).then((factory: ComponentFactory&lt;any&gt;) =&gt; {
              this.cmpRef = this.container.createComponent(factory);
          })
      }
  
      ngOnChanges() {
          this.updateComponent();
      }
  
      ngAfterViewInit() {
          this.isViewInitialized = true;
          this.updateComponent();
      }
  
      ngOnDestroy() {
          if(this.cmpRef)
              this.cmpRef.destroy();
      }
  }
  
  @Component({
      selector: 'container-component',
      template: `
          &lt;wrapper-component 
              *ngFor="let element of elements" 
              [type]="element"&gt;
          &lt;/wrapper-component&gt;
      `,
      directives: [WrapperComponent]
  })
  export class ContainerComponent {
  
      private elements: Type[] = [];    
  
      visibility: boolean = true;
  
      Add(element: Type) {
          this.elements.push(element);
      }
  
      AddAll(elements: Type[]) {
          elements.forEach(element =&gt; {
              this.Add(element);
          });
      }
  
      Clear() {
          this.elements = [];
      }
  }
  </code></pre>
  
  <p><strong>a1.component.ts</strong></p>
  
  <pre><code>import { Component, Type, ViewChild } from '@angular/core'
  import { ContainerComponent } from '../../components/container.component';
  
  @Component({
      selector: 'child-component',
      template: '&lt;div&gt;a{{text}}&lt;/div&gt;'
  })
  class ChildComponent {
      text: string;
  }
  
  @Component({
      selector: 'a1step',
      template: `
          &lt;button (click)="onClick()"&gt;Add&lt;/button&gt;
          &lt;container-component #container&gt;&lt;/container-component&gt;
      `,
      directives: [ContainerComponent]
  })
  export class A1Step {
      @ViewChild('container') container : ContainerComponent;
  
      onClick() {
          this.container.Add(ChildComponent);
      }
  }
  </code></pre>
  
  <p>Here I can dynamically add ChildComponents, but how can I set its text?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I don't know if could be useful to others, but defining an argument class for the ChildComponent and updating the elements array of the ContainerComponent as an array of objects containing the specific args, I can easily pass specific arguments to the ChildComponents:</p>
  
  <p><strong>container.component.ts</strong></p>
  
  <pre><code>import {
      Component,
      OnChanges,
      AfterViewInit,
      OnDestroy,
      Input,
      Type,
      ViewChild,
      ViewContainerRef,
      ComponentRef,
      ComponentResolver,
      ComponentFactory
  } from '@angular/core';
  
  export class ChildArgs {
      type: Type;
      args: any;
  }
  
  @Component({
      selector: 'wrapper-component',
      template: '&lt;div #container&gt;&lt;/div&gt;'
  })
  class WrapperComponent implements OnChanges, AfterViewInit, OnDestroy {
  
      @Input() argsElement: ChildArgs;
      @ViewChild('container', {read: ViewContainerRef}) container;
  
      cmpRef: ComponentRef&lt;any&gt;;
  
      private isViewInitialized: boolean = false;
  
      constructor(private resolver: ComponentResolver) { }
  
      private updateComponent() { 
          if(!this.isViewInitialized)
              return;
  
          if(this.cmpRef)
              this.cmpRef.destroy();
  
          this.resolver.resolveComponent(this.argsElement.type).then((factory: ComponentFactory&lt;any&gt;) =&gt; {
              this.cmpRef = this.container.createComponent(factory);
              this.cmpRef.instance.args = this.argsElement.args;
          })
      }
  
      ngOnChanges() {
          this.updateComponent();
      }
  
      ngAfterViewInit() {
          this.isViewInitialized = true;
          this.updateComponent();
      }
  
      ngOnDestroy() {
          if(this.cmpRef)
              this.cmpRef.destroy();
      }
  }
  
  @Component({
      selector: 'container-component',
      template: `
          &lt;wrapper-component 
              *ngFor="let argsElement of argsElements" 
              [argsElement]="argsElement"&gt;
          &lt;/wrapper-component&gt;
      `,
      directives: [WrapperComponent]
  })
  export class ContainerComponent {
  
      private argsElements: ChildArgs[] = [];
  
      AddArgsElement(argsElement: ChildArgs) {
          this.argsElements.push(argsElement);
      }
  }
  </code></pre>
  
  <p><strong>a1.component.ts</strong></p>
  
  <pre><code>import { Component, Type, ViewChild } from '@angular/core'
  import { ContainerComponent, ChildArgs } from '../../components/container.component';
  
  class ChildComponentArgs {
      text: string;
  }
  
  @Component({
      selector: 'child-component',
      template: '&lt;div&gt;a{{args.text}}&lt;/div&gt;'
  })
  class ChildComponent {
      args: ChildComponentArgs;
  }
  
  class ChildComponent2Args {
      text: string;
  }
  
  @Component({
      selector: 'child-component2',
      template: '&lt;div&gt;b{{args.text}}&lt;/div&gt;'
  })
  class ChildComponent2 {
      args: ChildComponent2Args;
  }
  
  @Component({
      selector: 'a1step',
      template: `
          &lt;button (click)="onClick()"&gt;Add&lt;/button&gt;
          &lt;button (click)="onClick2()"&gt;Add2&lt;/button&gt;
          &lt;container-component #container&gt;&lt;/container-component&gt;
      `,
      directives: [ContainerComponent]
  })
  export class A1Step {
      @ViewChild('container') container : ContainerComponent;
  
      private cnt: number = 0;
      private cnt2: number = 0;
  
      onClick() {
          let childComponentArgs: ChildComponentArgs = new ChildComponentArgs();
          childComponentArgs.text = "" + ++this.cnt; 
  
          let childArgs: ChildArgs = new ChildArgs();
          childArgs.type = ChildComponent;
          childArgs.args = childComponentArgs;
  
          this.container.AddArgsElement(childArgs);
      }
  
      onClick2() {
          let childComponentArgs: ChildComponent2Args = new ChildComponent2Args();
          childComponentArgs.text = "" + ++this.cnt2; 
  
          let childArgs: ChildArgs = new ChildArgs();
          childArgs.type = ChildComponent2;
          childArgs.args = childComponentArgs;
  
          this.container.AddArgsElement(childArgs);
      }
  }
  </code></pre>