1

I have a 3rd party library which dynamically creates a DOM element, I need to compile a component and insert it to the created DOM element.

I'm using component factory resolver and i know i can insert it to ViewContainerRef, but i'm not sure how to insert to plain DOM element created by outside angular lib.

onDynamicDomCreate: (e: GridChangeEvent) => {
            let factory = this.componentFactoryResolver.resolveComponentFactory(UserDetailsComponent);
            e.sender.element.createComponent(factory); // <--- how to properly create component here?
        }

1 Answer 1

4

You need to pass a DOM element when creating a component like this:

let factory = this.componentFactoryResolver.resolveComponentFactory(UserDetailsComponent);
const compRef = factory.create(injector, [], e.sender.element);

And also register the created view in the ApplicationRef, otherwise you won't have change detection for the component: appRef.attachView(compRef.hostView);

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

Comments

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.