Full answer
A custom element (Web Component) is a browser standard: you define a tag such as <school-rating>, and the browser creates it anywhere in HTML. Angular Elements (@angular/elements) wraps an Angular component so it works as one.
How to do it:
- Install
@angular/elements. - Create the element class:
const Rating = createCustomElement(RatingComponent, { injector }). The injector comes from the running application (for examplecreateApplication()orapp.injector). - Register it:
customElements.define('school-rating', Rating). - Use it in any HTML:
<school-rating value="4"></school-rating>.
Mapping rules:
- Each component input becomes a property of the element, and an attribute in dash case (
maxStarsbecomesmax-stars). Attributes are always strings, so use an input transform (numberAttribute) when needed. - Each output becomes a DOM
CustomEvent. The emitted value is inevent.detail. - Change detection runs for the element, and when the element is removed from the page it is destroyed, with cleanup.
Where it is useful: adding an Angular widget to a CMS or non-Angular page, sharing one component between apps built with different frameworks, and a step in migrations or micro-frontend setups where teams use different versions.
Limits and costs:
- Bundle size. Each Angular Elements bundle includes Angular's runtime. Several separate bundles on one page load the framework several times, unless you share one application and register many elements.
- Content projection is limited.
ng-contentworks at creation time, but dynamic projected content does not behave like in a normal Angular template. - Styling: with
ViewEncapsulation.ShadowDomstyles are isolated, withEmulatedthey use attributes. Global CSS may or may not reach the content. - No router, SSR support is limited, and zone.js or zoneless mode must be chosen once for the page.
- Older browsers need polyfills. All current browsers support custom elements.
Name elements with a dash and a prefix, as the standard requires, and document the properties and events as the public API.
import { Component, EnvironmentInjector, Injector, input, numberAttribute, output } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@Component({
selector: 'app-rating',
template: `
<span>Stars: {{ value() }}</span>
<button type="button" (click)="rated.emit(value() + 1)">Rate higher</button>
`,
})
export class RatingComponent {
readonly value = input(0, { transform: numberAttribute });
readonly rated = output<number>();
}
export function defineRatingElement(injector: Injector | EnvironmentInjector, tag: string): void {
const element = createCustomElement(RatingComponent, { injector: injector as Injector });
customElements.define(tag, element);
}
// In any HTML page after defineRatingElement(injector, 'school-rating'):
// <school-rating value="4"></school-rating>
// el.addEventListener('rated', (e) => console.log(e.detail));Angular 20.3 with `@angular/elements` 20.3. A Karma test defines the element, sets an attribute and a property, clicks the button, and reads the `CustomEvent` detail. Defining the same tag a second time threw a `NotSupportedError`.
Key points
- `createCustomElement(Component, { injector })` plus `customElements.define('tag-name', Element)`.
- Inputs become properties and attributes (strings). Outputs become `CustomEvent`s with data in `detail`.
- Good for embedding Angular widgets in other pages and for mixed framework setups.
- Costs: bundle size, projection and styling limits, no router, one runtime per page.
Common mistakes
- Forgetting that attribute values are strings.
- Loading a full Angular bundle several times on one page.
- Defining the same tag name twice (the browser throws an error).
Follow-ups
- How do you read an output value in plain JavaScript?
- How would you share Angular between several elements?
- When would you choose Module Federation instead?
Tips
- Show the four steps in order.
- Explain the mapping of inputs and outputs.
- Be honest about the costs.
Evaluation rubric
- Describes the packaging steps correctly.
- Explains input and output mapping.
- Names real limits and use cases.