Angular Elements provides the capability to package Angular components as custom elements (web components) that can be used in any HTML document, regardless of the framework. This allows Angular components to be consumed in non-Angular applications, server-rendered pages, or any context where standard HTML elements are used.
For information about creating standard Angular components, see Core Runtime Architecture. For server-side rendering, see Server-Side Rendering. For general security considerations when building web components, see Content Security and Sanitization.
Angular Elements bridges Angular's component model with the Web Components standard by transforming Angular components into browser-native custom elements. The transformation wraps Angular's dependency injection, change detection, and rendering infrastructure into a custom element that can be instantiated using standard DOM APIs.
Architecture Overview
Sources: adev/src/content/guide/elements.md1-169
The @angular/elements package exports several key types and functions for creating and managing custom elements.
Core Package Exports
Key Functions and Types:
| Symbol | Type | Purpose |
|---|---|---|
createCustomElement() | Function | Converts an Angular component into a constructor class for custom elements |
NgElement | Interface | Type representing a custom element created from an Angular component |
WithProperties<T> | Type | Helper type that adds component properties to NgElement |
NgElementConstructor | Interface | Constructor interface returned by createCustomElement |
NgElementStrategy | Interface | Internal strategy for managing component lifecycle |
Sources: adev/src/content/guide/elements.md36-62 adev/src/content/guide/elements.md104-162
The transformation from an Angular component to a custom element follows a specific process that involves metadata extraction, constructor creation, and registration with the browser.
Transformation Pipeline
Implementation Steps:
createCustomElement() function inspects the component's metadata to identify input properties and output events@Input({alias: 'name'})) are respectedNgElementConstructor is created that implements the custom element lifecycle callbackscustomElements.define()Sources: adev/src/content/guide/elements.md49-78
Angular Elements automatically maps component properties and events to Web Components standards.
Input Property Mapping
Output Event Mapping
Naming Conventions:
| Component Feature | Custom Element Equivalent | Example |
|---|---|---|
@Input() prop | prop attribute (dash-case) | inputValue → input-value |
@Input({alias: 'x'}) prop | x attribute (dash-case) | myProp → my-prop |
@Output() event | CustomEvent with type name | valueChanged → valueChanged event |
@Output({alias: 'x'}) event | CustomEvent with alias | myClick → myClick event |
Sources: adev/src/content/guide/elements.md63-78
Custom elements created with Angular Elements manage their own lifecycle and automatically bootstrap Angular's runtime infrastructure when attached to the DOM.
Custom Element Lifecycle
Self-Bootstrapping Behavior:
Custom elements created with Angular Elements are self-bootstrapping:
Sources: adev/src/content/guide/elements.md14-18
The Angular documentation includes a complete example demonstrating both traditional dynamic component loading and the custom elements approach.
Popup Service Architecture
Key Implementation Differences:
The traditional dynamic component loading approach (popup.service.ts18-42):
createComponent() callApplicationRefThe custom element approach (popup.service.ts44-59):
document.createElement()addEventListener()Type Safety with Custom Elements:
Sources: adev/src/content/examples/elements/src/app/popup.service.ts1-61 adev/src/content/guide/elements.md79-102
Angular Elements provides TypeScript types to enable type-safe interactions with custom elements.
Type Augmentation Pattern:
The WithProperties and NgElement types allow TypeScript to understand custom element properties:
Type Hierarchy:
| Type | Extends | Purpose |
|---|---|---|
NgElement | HTMLElement | Base interface for Angular-created custom elements |
WithProperties<T> | NgElement | Adds component-specific properties to NgElement |
HTMLElementTagNameMap | Native interface | Browser's type mapping for element tag names |
When HTMLElementTagNameMap is augmented, TypeScript automatically provides correct types for:
document.createElement('my-dialog') returns the correct typedocument.querySelector('my-dialog') returns the correct typeSources: adev/src/content/guide/elements.md104-162
Bootstrap Configuration:
Important Considerations:
The component's selector should not be used as the custom element tag name. Using the same name can cause Angular to create two component instances for a single DOM element:
Sources: adev/src/content/guide/elements.md59-62 adev/src/content/guide/elements.md86-102
Angular Elements has specific limitations related to the browser's custom element lifecycle:
Known Issues:
| Issue | Description | Affected Scenarios |
|---|---|---|
disconnect() callback | Problems with custom element disconnect behavior | Destroying and re-attaching elements |
| AngularJS integration | Issues when rendering in ng-if or ng-repeat | Hybrid AngularJS/Angular applications |
| Manual DOM manipulation | Detaching and re-attaching can cause issues | Dynamic DOM operations |
Recommended Practices:
display or visibility properties instead of removing from DOMSources: adev/src/content/guide/elements.md163-169
Dependencies on Core Angular Systems:
Angular Elements relies on several core Angular systems:
| System | Role in Angular Elements |
|---|---|
| Dependency Injection | Provides services to custom element components (guide/di) |
| Change Detection | Triggers updates when properties change (guide/change-detection) |
| Renderer | Performs DOM updates for the component (guide/rendering) |
| Compiler | Compiles component templates (must use AOT for production) (compiler) |
Security Considerations:
Custom elements inherit Angular's security model:
For detailed security considerations, see Content Security and Sanitization.
Sources: adev/src/content/guide/elements.md1-169 adev/src/content/guide/security.md1-370
Refresh this wiki