Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a component dynamically in angular

I went through angular dynamically loading components. But i could not find how to remove the component dynamically.

My requirement is that the chat application loads dynamic component(image/graph/list/table) as per the conversation. But how can i destroy the component if the conversation moves forward.

I am trying to destroy dynamic component with external event.

Please help how to proceed.

EDIT: https://stackblitz.com/angular/emjkxxxdxmk?file=src%2Fapp%2Fad-banner.component.ts

I developed my code according to this example. Instead of time interval, I need to use a API call from a service that is subscribed another component(chat component).

Below API response can load the component.I am looking for how to destroy the already loaded component i use the API call again.

public sendMessage(data): void {
    this.messages.push(this.message);
    this.API.getResponse(data).subscribe(res => {
      this.previousContext = res.context;
      console.log('res', res);
      if (res.result.type == 'table') {
        this.DataService.setTrigger(new AdItem(Table2Component, res));
      }
      this.messages.push(
        new Message(res.text, 'assets/images/bot.png', new Date(), 'chatbot')
      );
    });
    this.message = new Message('', 'assets/images/user.png', this.message.timestamp, 'user');
  }
like image 391
Sridhar Natuva Avatar asked Sep 16 '18 12:09

Sridhar Natuva


People also ask

How do you destroy dynamic components?

In our dynamic component loader, it will be load component using createComponent() of ViewContainerRef. The ComponentRef of the newly created component and call the clear() method of ViewContainerRef destroys the existing view in the container. Add the destroy component.

What is dynamic component in angular?

What dynamic components are. Dynamic means, that the components location in the application is not defined at buildtime. That means, that it is not used in any angular template. Instead, the component is instantiated and placed in the application at runtime.

Does angular router destroy component?

Expected behavior. Components should be destroyed by the angular router whether they throw an error or not.

What is NgComponentOutlet?

NgComponentOutletlinkInstantiates a Component type and inserts its Host View into the current View. NgComponentOutlet provides a declarative approach for dynamic component creation.


1 Answers

Use destroy() method

Destroys the component instance and all of the data structures associated with it.

ref:ComponentRef<any>;
loadComponent() {
    this.currentAdIndex = (this.currentAdIndex + 1) % this.ads.length;
    let adItem = this.ads[this.currentAdIndex];

    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(adItem.component);

    let viewContainerRef = this.adHost.viewContainerRef;
    viewContainerRef.clear();

    let componentRef = viewContainerRef.createComponent(componentFactory);
    this.ref=componentRef;
    (<AdComponent>componentRef.instance).data = adItem.data;

  }

  removeComponent(){
   try{
       this.ref.destroy();
   }
   catch(e){
   }
  }

Example:https://stackblitz.com/edit/destroy?file=src/app/ad-banner.component.ts

like image 183
Chellappan வ Avatar answered Oct 08 '22 14:10

Chellappan வ