Shipping Angular Elements as Web Components

Shipping Angular Elements as Web Components

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way. -

Shipping Angular Components as Web Components is easy as pie. You just have to add the @angular/elements package and use the createCustomElement() API.

ng add @angular/elements

Next up is to create an actual component. It's a subscription component and somewhat configurable. Users can enter their email addresses and submit to get registered with a subscription plan.

The following goes into the script file i.e. the component file,

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { SubscriptionService } from '../shared/subscriber.service';

  selector: 'app-subscriber',
  templateUrl: './subscriber.component.html',
  styleUrls: ['./subscriber.component.scss']
export class SubscriberComponent implements OnInit {
  @Input() url: string;
  @Output() subscribed: EventEmitter<string> = new EventEmitter<string>();

  subscriptionForm: FormGroup;

  get email() {
    return this.subscriptionForm.get('email');

    private fb: FormBuilder,
    private subscriptionSvc: SubscriptionService
  ) {}

  ngOnInit() {

  createForm() {
    this.subscriptionForm =
        email: ['', [Validators.required,]]
      { updateOn: 'submit' }

  onSubmit() {
    if (this.subscriptionForm.status === 'VALID') {
        .subscribe(res => this.subscribed.emit(res));
    } else {

And the markup is simple as this,

<div class="card">
  <div class="card-content">
    <div class="content">
      <form [formGroup]="subscriptionForm" novalidate (ngSubmit)="onSubmit()">
        <div class="field is-grouped">
          <p class="control is-expanded">
              class="input is-light is-warning"
                'is-danger': email.invalid && (email.dirty || email.touched)
              placeholder="Your email address"
          <p class="control">
              class="button is-warning is-light is-outlined"

Singleton service class is doing a mock delay request and returning a string containing a notification. You can use the HttpClient to make a post request to your preferred server. Of course, the URL is configurable here, and it can be set by the consumer of the component.

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { delay } from 'rxjs/operators';

  providedIn: 'root'
export class SubscriptionService {
  constructor() {}

  // Create a POST request using the url and register email in the backend
  register(url: string, email: string): Observable<string | undefined> {
    return of(
      `Please check your email (${email}) and confirm subscription`

The bridging of Angular component to web component happens in the app.module.ts.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';

import { SubscriberComponent } from './subscriber/subscriber.component';

  declarations: [SubscriberComponent],
  imports: [BrowserModule, ReactiveFormsModule],
  providers: [],
  entryComponents: [SubscriberComponent]
export class AppModule {
  constructor(injector: Injector) {
    const SubscriberElement = createCustomElement(SubscriberComponent, {

    customElements.define('subscriber-element', SubscriberElement);

  ngDoBootstrap() {}

Things to notice are:

  • How the createCustomeElement API is used to create a custom element.
  • customElements.define() is used to register the element to browser's CustomElementRegistry.
  • Components are registered using the entryComponents option so that they can be dynamically loaded into the view
  • ngDoBootstrap is used for manual bootstrapping of the application instead of using the bootstrap array.
Transforming a component to a custom element makes all of the required Angular infrastructure available to the browser -

You can do a production build of the app and get the minified styles and scripts from the dist folder. You can reference these files in any other HTML project and start using the subscriber-element.

For quick testing, you can also use the index.html file that resides in your Angular app. The following markup shows how to use the element in plain HTML

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="icon" type="image/x-icon" href="favicon.ico" />
    <div class="section">
      <div class="container">
        <h1 class="title">Subscription Form</h1>
        <h2 class="subtitle">
          A web component created with <code>@angular/elements</code> project
    <div class="section">
      <div class="container">
        <h2 class="subtitle">Console</h2>
      var el = document.querySelector('subscriber-element');
      el.addEventListener('subscribed', evt => {
        const pre = document.querySelector('pre');
        pre.innerText += `Log: ${evt.detail}\n`;


Contribute to fiyazbinhasan/ng-playground development by creating an account on GitHub.


Image Upload in Angular with Preview & Progress
In Angular, you can upload a file and at the same time get the progress by enabling the reportProgress flag on HttpRequest object. Source code available in Github.
Angular Form Validation on Blur and Submit
updateOn: ‘blur’ and updateOn: ‘submit’ are the options we were really craving for while working with Angular Forms. Now it’s available out of the box and you don’t need any custom implementations.
Asynchronous Validation in Angular
It’s very easy to setup asynchronous validation in Angular Form. You can check wheather provided email/date/name etc. already exists in the server with a HTTP call.