0

When using Angular Elements, we'd usually have something like this to build an app as a web component:

export class AppModule implements DoBootstrap {

    ngDoBootstrap(appRef: ApplicationRef): void {
        customElements.define('foo', createCustomElement(AppComponent, {injector: appRef.injector}));
    }
}

With the above, when a host tag is inserted in the host application, the relevant DOM fragment looks like this:

...
<foo>
    #shadow-root
        (AppComponent's contents)

Is there a way to inject a reference (like ElementRef) to the <foo> element hosting the web component into AppComponent's constructor?

yktoo
  • 2,696
  • 1
  • 23
  • 35

1 Answers1

0

As it turns out, the ElementRef injected into the constructor points exactly to that element (the <foo> tag), which is a parent to Shadow DOM:


constructor(private readonly el: ElementRef) {
    // el.nativeElement is <foo>
}
yktoo
  • 2,696
  • 1
  • 23
  • 35