2

I store a dynamically generated HTMLElement in a variable in my component class. I would like to display it, but I have no idea how is that possible. I tried it like this:

Inside the component class:

elem: HTMLElement;

In the template:

<ng-container *ngTemplateOutlet="elem"></ng-container>

I get this error:

TypeError: templateRef.createEmbeddedView is not a function

2 Answers 2

2

If you have a naked HTMLElement, there's nothing Angular-y you can do. NgTemplateOutlet expected a TemplateRef, not a vanila HTMLElement object.

To insert an HTML Element into the DOM, simply use the appropriate DOM method for that. Which one you'll use entirely depends on the place where you want to use it.

For example, you could do

const wrapper = document.querySelector('div.wrapper')
wrapper.append(el) // pass in your element

You can also grab the reference to the wrapper in a more Angular way, by adding an Angular reference in the template on the element you want to select,

<div class="wrapper" #wrapper></div>

and using ViewChild decorator on a property where you want Angular to assign the ElementRef to.

@ViewChild('wrapper', { static: true, read: ElementRef })
public wrapperElRef: ElementRef<HTMLDivElement>

Once the view is initialized (you'll be notified when this happens via the ngAfterViewInit hook), you can grab the HTML element within by accessing `.nativeElement:

this.wrapperElRef.nativeElement //: HTMLDivElement

You can now do

this.wrapperElRef.nativeElement.append(elem)

That said, unless you know what you're doing (usually wrapping an existing library or integrating with an existing large system that you cannot yet refactor completely)

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

1 Comment

I think the first and last examples (manually calling HTMLElement#append()) should be avoided, since messing with the DOM outside of Angular's scope can cause misalignment between Angular's render tree and the actual DOM. Angular provides Renderer2, a built-in service which allows to to modify the DOM manually, while allowing Angular to keep track of the DOM. It has a function named appendChild which you can use.
0

Most of the times it depends how you have created the html. I suggest specifying in comments where are you receiving the variable which holds the HTMLElement, so I could give you the most effective way to do it.

But generally - try using innerHTML binding in the parent element.

Typescript:

public elem: HTMLElement;

public getElement(): string {
   return this.elem.innerHTML;
}

HTML:

<div innerHTML="getElement()"></div>

3 Comments

The element was created by a service, not in the component.
@IterAtor But how? What methods have you used? Did you took it from ElemntRef? (#id in othe component)? Also, did my solution work?
No, it was created with document.createElement

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.