Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parent.appendChild is not a function

Tags:

angular

I want to use the renderer2's append child function but I'm having some difficulties moving whole components. Here's a shortened versions of my component.

ts:

@ViewChild('test', { static: false }) test;
@ViewChild('test2', { static: false }) test2;

this.renderer.appendChild(this.test, this.test2);

html:

<div #test></div>

<my-component #test2></my-component>

So essentially I just want to move </my-component> into the div above it using the appendChild function. But when I execute it, I get the error:

parent.appendChild is not a function

Any ideas why it's erroring?

like image 645
cup_of Avatar asked Dec 14 '22 10:12

cup_of


1 Answers

You should pass DOM elements to this method:

@ViewChild('test', { static: false }) test: ElementRef;
@ViewChild('test2', { static: false, read: ElementRef }) test2: ElementRef;
...

this.renderer.appendChild(this.test.nativeElement, this.test2.nativeElement);
like image 95
yurzui Avatar answered Feb 15 '23 01:02

yurzui