← Back to Angular, page 9

angular-0178 · Medium mid

What is Angular Elements, how do you package a component as a custom element, and what are its limits?

Angular Elements converts an Angular component into a standard custom element (Web Component) with createCustomElement and customElements.define. Inputs become properties and attributes, outputs become DOM events, and the tag works in any page or framework. The cost is that each element bundles Angular unless the apps share one runtime, and projection and styling have limits.

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:

  1. Install @angular/elements.
  2. Create the element class: const Rating = createCustomElement(RatingComponent, { injector }). The injector comes from the running application (for example createApplication() or app.injector).
  3. Register it: customElements.define('school-rating', Rating).
  4. 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 (maxStars becomes max-stars). Attributes are always strings, so use an input transform (numberAttribute) when needed.
  • Each output becomes a DOM CustomEvent. The emitted value is in event.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-content works at creation time, but dynamic projected content does not behave like in a normal Angular template.
  • Styling: with ViewEncapsulation.ShadowDom styles are isolated, with Emulated they 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.

typescript
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.