Blog>
Snippets

Angular Elements with Angular Router

Show how to use Angular Elements in applications with Angular Router, focusing on navigation within the shadow DOM of a custom element.
import { NgModule, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { createCustomElement } from '@angular/elements';
import { AppComponent } from './app.component';
import { CustomElementComponent } from './custom-element/custom-element.component';

const routes: Routes = [
    { path: '', component: AppComponent },
    // Additional route configurations
];

@NgModule({
    declarations: [
        AppComponent,
        CustomElementComponent
    ],
    imports: [
        BrowserModule,
        RouterModule.forRoot(routes)
    ],
    providers: [],
    bootstrap: [AppComponent],
    entryComponents: [CustomElementComponent]
})
export class AppModule {
    constructor(private injector: Injector) {
        const el = createCustomElement(CustomElementComponent, { injector });
        customElements.define('custom-element', el);
    }
    // Override ngDoBootstrap to prevent automatic bootstrap
    ngDoBootstrap() {}
}
This code registers a custom element within the Angular module and defines a route. 'createCustomElement' is used to convert 'CustomElementComponent' into a custom element which can then be defined in the browser's CustomElementRegistry using 'customElements.define'. The 'ngDoBootstrap' method is overridden to prevent automatic bootstrapping of the application since the Angular app is being bootstrapped manually when the custom element is used.
import { Component, Input, Injector, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
    selector: 'app-custom-element',
    templateUrl: './custom-element.component.html',
    styleUrls: ['./custom-element.component.css']
})
export class CustomElementComponent implements OnInit {
    @Input() someInput: string;

    constructor(private injector: Injector, private router: Router) {
        // Inject the Angular router
    }

    ngOnInit(): void {
        // Handle the router initialization within the shadow DOM
    }

    navigateTo(path: string): void {
        this.router.navigate([path]);
    }
}
This code defines an Angular component that will be used as a custom element. An instance of the Router is injected into the constructor to allow the custom element to navigate within the Angular application. The 'navigateTo' method provides functionality to navigate to different routes when called.
document.querySelector('custom-element').addEventListener('some-event', () => {
    // Access the Angular injector
    const injector = document.querySelector('custom-element').injector;

    // Retrieve the Angular Router from the injector
    const router = injector.get(Router);

    // Perform navigation
    router.navigate(['/some-path']);
});
This code demonstrates how to interact with the Angular Router from outside the Angular context, for example, in a surrounding DOM of the custom element. It uses the injector associated with the custom element to retrieve the Router and then navigates to '/some-path' when 'some-event' is triggered.