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?

1
  • ?? isn't that what this.getRootNode().host does ?? (I ditched Angular when they called 2.0 an Upgrade) Commented Dec 8, 2022 at 17:18

1 Answer 1

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>
}
Sign up to request clarification or add additional context in comments.

Comments

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.