Diemauerdk
Diemauerdk

Reputation: 5938

Angular - using how to call function on component created dynamically using ng-dynamic-component

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 :)

Upvotes: 0

Views: 1521

Answers (1)

Markus Dresch
Markus Dresch

Reputation: 5574

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.

Upvotes: 3

Related Questions