What is @angular/elements?
@angular/elements allows Angular components to be used as custom elements (web components). This enables the integration of Angular components into non-Angular applications and enhances reusability across different frameworks and libraries.
What are @angular/elements's main functionalities?
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>
Other packages similar to @angular/elements
stencil
Stencil is a compiler that generates Web Components (custom elements) and builds high-performance web apps. It is framework-agnostic and can be used to create reusable components that work with any major framework or no framework at all. Compared to @angular/elements, Stencil is more focused on performance and offers additional features like lazy loading and pre-rendering.
lit
Lit is a simple library for building fast, lightweight web components. It provides a minimal API to define custom elements and manage their lifecycle. Lit is known for its small size and ease of use, making it a good alternative to @angular/elements for projects that require lightweight and fast custom elements.
vue-custom-element
vue-custom-element is a Vue plugin that enables the creation of custom elements from Vue components. It allows Vue components to be used as web components in non-Vue applications. This package is similar to @angular/elements but is tailored for Vue.js, providing a seamless way to integrate Vue components as custom elements.
19.1.0 (2025-01-15)
common
| Commit | Type | Description |
| -- | -- | -- |
| e4c50b3bea | feat | expose component instance in NgComponentOutlet (#58698) |
compiler
| Commit | Type | Description |
| -- | -- | -- |
| ceadd28ea1 | fix | allow $any in two-way bindings (#59362) |
| aed49ddaaa | fix | use chunk origin in template HMR request URL (#59459) |
compiler-cli
| Commit | Type | Description |
| -- | -- | -- |
| c5c20e9d86 | fix | check event side of two-way bindings (#59002) |
core
| Commit | Type | Description |
| -- | -- | -- |
| d010e11b73 | feat | add event listener options to renderer (#59092) |
| 57f3550219 | feat | add utility for resolving defer block information to ng global (#59184) |
| 22f191f763 | feat | extend the set of profiler events (#59183) |
| e894a5daea | feat | set kind field on template and effect nodes (#58865) |
| bd1f1294ae | feat | support TypeScript 5.7 (#58609) |
| 9870b643bf | fix | Defer afterRender until after first CD (#58250) |
| a5fc962094 | fix | Don't run effects in check no changes pass (#58250) |
migrations
| Commit | Type | Description |
| -- | -- | -- |
| d298d25426 | feat | add schematic to clean up unused imports (#59353) |
| 14fb8ce4c0 | fix | resolve text replacement issue (#59452) |
platform-browser
| Commit | Type | Description |
| -- | -- | -- |
| 8c5db3cfb7 | fix | avoid circular DI error in async renderer (#59256) |
router
| Commit | Type | Description |
| -- | -- | -- |
| 52a6710f54 | fix | complete router events
on dispose (#59327) |
<!-- CHANGELOG SPLIT MARKER -->
<a name="19.0.7"></a>