Share with   

Angular Custom Web Element to Embedded in Any HTML Websites or Frameworks.

Angular Web Elements are self-contained, encapsulated components that can be embedded in any HTML page. This empowering component reusability across frameworks.


Angular Custom Web Element


Angular Web Elements: Empowering Component Reusability Across Frameworks

In the world of web development, building reusable components is crucial for creating scalable and maintainable applications. However, sharing components across different frameworks and technologies can be a daunting task. That's where Angular Web Elements come into play. Angular Web Elements allow you to package Angular components as custom elements, making them usable in any web application, regardless of the underlying framework.


What are Angular Web Elements?

Angular Web Elements, also known as Angular Elements, are self-contained, encapsulated Angular components that can be embedded in any HTML page. They are built using the Angular framework and then converted into custom elements using Angular's built-in capabilities.

Custom elements, a web standard for defining new HTML elements, provide a way to extend the vocabulary of HTML. With Angular Web Elements, you can create custom elements from your Angular components and use them seamlessly in any web application that supports standard HTML tags.


Benefits of Angular Web Elements

  1. Framework Agnostic: Angular Web Elements allow you to use Angular components in applications built with any framework or even with vanilla JavaScript. By packaging components as custom elements, you can leverage the power of Angular's component-based architecture without being tied to a specific framework.

  2. Reusability: With Angular Web Elements, you can easily share your Angular components across different projects and teams. This promotes code reuse and reduces duplication efforts, saving development time and effort.

  3. Compatibility: Angular Web Elements are compatible with modern web browsers, ensuring broad support across various platforms. They can be used in both Angular applications and non-Angular applications, making them versatile and adaptable.

  4. Isolation and Encapsulation: Angular Web Elements encapsulate their own styles, templates, and behavior, providing a self-contained unit that can be used independently. This isolation prevents style or behavior conflicts when integrating components into different projects.


Creating Angular Web Elements

To create an Angular Web Element, you need to follow a few steps:

  1. Set up an Angular project: Start by setting up an Angular project using the Angular CLI or any other preferred method.

  2. Create a component: Build the desired component that you want to convert into a web element. Ensure that the component is self-contained and does not rely on any external dependencies or services.

  3. Convert the component into a web element: Use Angular's built-in capabilities to convert the component into a custom element. This involves defining the component as a custom element using Angular's createCustomElement function and registering it with the browser's custom elements registry.

  4. Build and package the web element: Build the Angular project and package the web element for distribution. The resulting output will be a JavaScript file that includes the component and its dependencies, ready to be used in any web application.


Step 1: Set up an Angular project Start by setting up a new Angular project using the Angular CLI. Open your terminal or command prompt and run the following command:

ng new my-element-project

This command will create a new Angular project with the name "my-element-project" in a directory of the same name. Navigate to the project directory using the following command:

cd my-element-project


Step 2: Create a new Angular component Next, create a new Angular component that you want to convert into an Angular element. Use the Angular CLI to generate the component by running the following command:

ng generate component my-element

This command will generate a new component called "my-element" along with the necessary files and folder structure.


Step 3: Configure the Angular project for element generation To configure your Angular project for element generation, you need to make a few changes to the project's configuration files.

Open the "angular.json" file in the root directory of your project. Locate the "projects" section and find the configuration for your project (in this case, "my-element-project"). Inside the configuration object, add the following property:

"architect": {
  "build": {
    "options": {
      "outputPath": "dist/my-element-project",
      "scripts": [],
      "styles": [],
      "assets": []

This configuration ensures that the build output is generated in the "dist/my-element-project" directory and excludes any unnecessary scripts, styles, and assets.


Step 4: Convert the Angular component into an Angular element Now, it's time to convert the Angular component you created earlier into an Angular element. Open the component file (e.g., "my-element.component.ts") and modify it as follows:

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { createCustomElement } from '@angular/elements';

  selector: 'my-element',
  template: `
    <!-- Your component's template here -->
    <button (click)="sendMessage()">Send Message</button>
  styleUrls: ['./my-element.component.css'],
  encapsulation: ViewEncapsulation.ShadowDom
export class MyElementComponent implements OnInit {
  @Input() message: string;
  @Output() messageSent = new EventEmitter<string>();

  constructor() { }

  ngOnInit() {

  sendMessage() {
    this.messageSent.emit('Hello, message coming from angular web element child element!');



Step 5: Register and create the web element in the module file In the module file (e.g., app.module.ts), you will register and create the web element.

npm install @angular/elements --save

Use this command to install angular element support in your application.

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

import { MyElementComponent } from './my-element.component';

  declarations: [MyElementComponent],
  imports: [BrowserModule],
  entryComponents: [MyElementComponent]
export class AppModule {
  constructor(private injector: Injector) {
    const MyElementElement = createCustomElement(MyElementComponent, { injector });
    customElements.define('my-element', MyElementElement);

  ngDoBootstrap() {}

In this code snippet, we import the necessary dependencies, define the component selector as 'my-element', specify the component's template and styles, and encapsulate the styles using Shadow DOM.

We then use the createCustomElement function from the @angular/elements package to convert the component into an Angular element. Finally, w

e register the element with the browser's custom elements registry using customElements.define.


Step 6: Build the Angular element To build the Angular element, run the following command:

ng build --prod --output-hashing none

This command builds the Angular project in production mode and disables output hashing, ensuring consistent filenames for easier usage.


Step 7: Use the Angular element in a web application After the build is complete, navigate to the "dist/my-element-project" directory. You will find the built Angular element files there.

To use the Angular element in a web application, include the generated JavaScript file (e.g., "main.js") in your HTML page and use the custom element tag. For example:

    <script src="main.js"></script>
    <my-element message="Hello"></my-element>

Replace "main.js" with the actual filename of the generated JavaScript file.


That's it! You have successfully created an Angular element using the Angular CLI. The custom element <my-element> will render the Angular component encapsulated within it.


Note: Make sure to handle any necessary polyfills and browser support considerations based on your target audience and project requirements.

Remember to consult the Angular documentation and official resources for any further details or specific requirements regarding Angular elements and their usage.


Using Angular Web Elements

Once you have created an Angular Web Element, using it in other projects is straightforward. Simply include the JavaScript file generated during the packaging process and use the custom element as you would any other HTML element.

For example, if you created a custom element called <my-button>, you can use it in an HTML page like this:

The custom element <my-button> will render the Angular component encapsulated within it, regardless of the underlying framework of the host application.



Angular Web Elements offer a powerful solution for sharing Angular components across different frameworks and technologies. By converting Angular components into custom elements, developers can achieve component reusability and interoperability, leading to more efficient development workflows and better code organization.

Whether you're building a large-scale enterprise application or a small website, Angular Web Elements provide a way to leverage the benefits of Angular's component-based architecture while ensuring compatibility and flexibility across the web ecosystem.

Author Image
Guest User