Angular 4 - Remove current dynamic component programmatically

Viewed 2852

I have created some dynamic components like -

<div>
  <ang-textfield></ang-textfield>
  <ang-textarea></ang-textarea>
  <ang-checkbox></ang-checkbox>
</div>

Within specific dynamic component there are some elements like -

<ang-textfield>
  <input type="text" />
  <button>Settings</button>
  <button (click)="remove_component()">Remove</button>
</ang-textfield>

I want to remove current component after clicking Remove button.

How can i do that?

Here is the plunker

1 Answers

You can Pass the component factory as a Reference to the Dynamic Build Component and then in the Dynamic Component Have a button Destroy .

This Gist will give you a Clearer Idea.

A live Example

Related