1

Please refer the plunker, in that you can notice the child component "Loader" is loaded multiple times whenever the button is clicked i.e. on every click a new instance is being created.

How do I avoid creating multiple instance of the same component? My requirement whenever the button is clicked the new instance should replace the existing? how to do this?

Parent component

import { Component, ViewChild, ViewContainerRef, ComponentResolver, Injector, AfterViewInit } from '@angular/core';
import {Loader} from './Loader';
@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <input type="button" (click)="onclick($event)" value="Click"/>
      <h3>Loading component</h3>
      <load></load>
    </div>
  `,
  directives: [Loader]
})
export class App {
  constructor(private _injector: Injector, private viewContainer: ViewContainerRef, private _cr: ComponentResolver) {
    this.name = 'Angular2 (Release Candidate!)';
    console.log("in constructor of App");
  }
   @ViewChild(Loader, { read: ViewContainerRef }) childContainer;

  onclick(event)
  {
     this._cr.resolveComponent(Loader).then(cmpFactory => {
          console.log("Creating component");

          this.childContainer.createComponent(cmpFactory,null, this._injector);
          let cmpRef = this.childContainer.createComponent(cmpFactory);
          cmpRef.instance.ParentID = "55";  

      });

  }


}

1 Answer 1

2

Store cmpRef and call cmpRef.destroy() before you create a new instance

  onclick(event)
  {
     this._cr.resolveComponent(Loader).then(cmpFactory => {
          if(this.cmpRef) {
            this.cmpRef.destroy();
            this.cmpRef = null;
          }
          console.log("Creating component");

          //this.childContainer.createComponent(cmpFactory,null, this._injector);
          this.cmpRef = this.childContainer.createComponent(cmpFactory);
          this.cmpRef.instance.ParentID = "55";  

      });

  }
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please correct it in the plunker plnkr.co/edit/8H7oz8Yeia9F3NQ3sgyi?p=preview The suggested changes is not working in it?
There are two variants of createComponent in the Plunker, one with passing the injector and one without. destroy() is only called for the one where cmpRef is stored. Therefore for each click two components were created but only one destroyed. I commented out one createComponent plnkr.co/edit/VyFdZOT8CoElkSQ129jc?p=preview
understood the mistake, but even now if you notice there are 2 instances of the Loader component is rendered i.e. first instance during the time of page load and the second during the click event. How to avoid during the page load i.e. my need is to load only during the click event?
One is added statically by <load></load> in the AppComponent template. Just delete this line ;-)
I tried deleting that line already before posting, in that case it doesn't load any instance. even tried adding a div tag with id as "load". <div id="load"></div>. In both cases it doesn't even load any single instance. ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.