2

I have a banner that is loaded via an API call in my application that I don't have direct access to the template for. Due to the nature of the application I need to inject some code into it after it has been appended to the DOM. I'd like to do something like this once it has been loaded to place my own component among the banner:

const customLogoElem = document.getElementsByClassName('custom-logo').item(0);
customLogoElem.innerHTML = `<my-component></my-component>`;

The Angular element renders in the DOM but it doesn't include the template from <my-component />, it's instead just blank. Is there some way I can do this?

3
  • Inserting the tag for an Angular component into the DOM does not automatically instantiate the component. You will need to manually create the component using something like ViewContainerRef.createComponent(). Commented Feb 11, 2021 at 15:06
  • 1
    Does this answer your question? How to dynamically add innerHTML with angular 2 components Commented Feb 11, 2021 at 15:08
  • Sorta? I understand the concept of using createComponent to insert a component dynamically, but it requires something for ViewChild to reference. I can't simply set customLogoElem.innerHTML = '<div #target></div>' and run createComponent as I run into the same issue I was in before, it will undefined to Angular. Commented Feb 11, 2021 at 16:20

0

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.