Creating a Custom Element
This code demonstrates how to create a custom element from an Angular component. The `createCustomElement` function is used to convert `MyComponent` into a custom element named 'my-element'.
import { Injector, NgModule } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { BrowserModule } from '@angular/platform-browser';
import { MyComponent } from './my-component.component';
@NgModule({
declarations: [MyComponent],
imports: [BrowserModule],
entryComponents: [MyComponent]
})
export class AppModule {
constructor(private injector: Injector) {
const myElement = createCustomElement(MyComponent, { injector });
customElements.define('my-element', myElement);
}
ngDoBootstrap() {}
}
Using the Custom Element in HTML
Once the Angular component is converted into a custom element, it can be used in any HTML file just like a standard HTML element. This example shows how to include and use the custom element 'my-element' in an HTML file.
<!DOCTYPE html>
<html>
<head>
<title>Angular Element</title>
<script src="path/to/your/angular-element.js"></script>
</head>
<body>
<my-element></my-element>
</body>
</html>