| Start |
|---|
| A wrapper component that adds dynamic components declaratively A custom component that takes the type of a component as input and creates an instance of that component type inside itself. When the input is updated, the previously added dynamic component is removed and the new one added instead. This allows you to create dynamic components like |
| Dynamically add component on specific event(click) Main Component File: childComp.ts : app.module.ts : |
| Rendered dynamically created component array on template html in Angular2 We can create dynamic component and get the instances of component into an array and finally rendered it on template. For example, we can can consider two widget component, ChartWidget and PatientWidget which extended the class WidgetComponent that I wanted to add in the container. ChartWidget.ts chart-widget.compoment.html (using primeng Panel) DataWidget.ts data-widget.compoment.html (same as chart-widget using primeng Panel) WidgetComponent.ts we can creat dynamic component instances by selecting the pre-existing components. For example, Finally we use it in app component. app.component.ts app.component.html https://plnkr.co/edit/lugU2pPsSBd3XhPHiUP1?p=preview (opens new window) Some modification by @yurzui to use mouse event on the widgets view.directive.ts import { ViewRef, Directive, Input, ViewContainerRef } from '@angular/core'; app.component.ts app.component.html https://plnkr.co/edit/JHpIHR43SvJd0OxJVMfV?p=preview (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference