Using Renderer2 removeChild with parent set as null

Discussão em 'Angular' iniciado por Matt Saunders, Março 12, 2025.

  1. I'm using Renderer2 to render some custom elements in an Angular application. As each element is created, it is stored as a constant:

    const myNewElement = this.renderer2.createElement('div');


    When the time comes to remove this element, I am using removeChild. This has a parameter for the parent of the node being removed:

    removeChild(parent: any, oldChild: any, isHostElement?: boolean | undefined)


    However if I set parent to null, it seems to remove oldChild from the DOM successfully and without throwing an error:

    this.renderer2.removeChild(null, myNewElement); // Works!


    Could this approach cause any potential issues? Should a parent element be specified, even if removeChild works without it?

    Continue reading...

Compartilhe esta Página