Angular elements are reusable components that have been transformed into custom elements (also called Web Components). Angular provides a simple and effective method to create Web components.
Web components are custom HTML elements available in native HTML specifications to extend the features/tags of the HTML document. It can be created through JavaScript and can be made available in the HTML document. JavaScript has special methods to create custom HTML elements.
Creating custom HTML elements using JavaScript is a lengthy process and developers need to understand the internal workings of custom HTML elements and the Shadow DOM concept. Angular simplifies the process by enabling the conversion of Angular components to web components with minimal change in the component class.
Creating Angular Elements
Creating Angular elements means transforming a component into a custom element. For this purpose, the @angular/elements package is used. This package exports a createCustomElement() function that converts a component into a class that can be registered with the browser as a custom element.
Example
Let us learn how to create a custom HTML element in Angular.
In this example, we will create a component to display employee information (say EmpCard) and convert it into custom HTML element.
Step 1: Create an angular application, emp-card-web-component using Angular CLI.
ng newemp-card-web-component
Step 2: Create the emp-card component using Angular CLI.
Step 3: Add encapsulation option in the @Component decorator with ViewEncapsulation.ShadowDom option. ShadowDom option enables the HTML native ShadowDom concept to preserve the styles of the component without leaking into the other part of the HTML document.
Step 8: Next, install @angular/elements module provided by angular team. @angular/elements module has options to create custom HTML element from angular component.
ng add @angular/elements
✔ Determining Package Manager
Using package manager: npm
✔ Searching for compatible package version
Found compatible package version: @angular/[email protected].
✔ Loading package information from registry
✔ Confirming installation
✔ Installing package
Package "@angular/elements" was found but does not support schematics.
Step 9: Next, open the main.ts file and remove all boilerplate code.
Step 10: Next, import createApplication from @angular/platform-browser. createApplication will bootstrap the angular application.
Step 11: Next, import createCustomElement from @angular/element module. createCustomElement will be used to create custom HTML element from the angular component.
Step 13: Next, create application using createAppliation() method by inputting providers and a callback method. The callback method will be used to create the custom HTML element from angular component.
Step 14: Implement the callback method and create custom element using createCustomElement() method. createCustomElement accepts the component to be converted and the apps injector.
We can get injector from application reference returned from createApplication() method.
Step 18: Once, the build is done, the output files are available in dist/emp-card-web-component folder. It has below files (similar files) along with a index.html file.
main-5LWF45YA.js
polyfills-FFHMD2TL.js
styles-5INURTSO.css
Step 19: Update the index.html file of the src folder with the newly created component and check the output for correctness.
Leave a Reply