0

I am using the npm package ng-dynamic-component to create dynamic components.

I am in a situation where i want to call a specific function on the dynamically created component using this package.

I have experimented with a lot of different ways but so far found no solution.

Does anyone have any idea if it is possible to call a function on a component that is created dynamically using the mentioned package?

Thanks :)

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Diemauerdk
  • 5,238
  • 9
  • 40
  • 56

1 Answers1

3

ng-dynamic-component has a "Component Creation Event" ndcDynamicCreated that passes a ComponentRef<any> as a parameter.

From the docs:

@Component({
  selector: 'my-component',
  template: `<ndc-dynamic [ndcDynamicComponent]="component"
                          (ndcDynamicCreated)="componentCreated($event)"
                          ></ndc-dynamic>`
})
class MyComponent {
  component = MyDynamicComponent1;
  componentCreated(compRef: ComponentRef<any>) {
    // utilize compRef in some way ...
  }
}

Utilizing compRef in some way would be calling a function of the compRef.instance property in your case.

Markus Dresch
  • 5,290
  • 3
  • 20
  • 40
  • I have tested it out and can confirm that on the componentref generated i am able to take .instance and then call a function on the dynamically created component. Thanks! – Diemauerdk Apr 30 '19 at 07:13