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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.