{ "id": "guide/elements", "title": "Angular elements overview", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Angular elements overviewlink

\n

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.

\n
\n

For the sample app that this page describes, see the .

\n
\n

Custom elements are a Web Platform feature currently supported by Chrome, Edge (Chromium-based), Firefox, Opera, and Safari, and available in other browsers through polyfills (see Browser Support).\nA custom element extends HTML by allowing you to define a tag whose content is created and controlled by JavaScript code.\nThe browser maintains a CustomElementRegistry of defined custom elements, which maps an instantiable JavaScript class to an HTML tag.

\n

The @angular/elements package exports a createCustomElement() API that provides a bridge from Angular's component interface and change detection functionality to the built-in DOM API.

\n

Transforming a component to a custom element makes all of the required Angular infrastructure available to the browser.\nCreating a custom element is simple and straightforward, and automatically connects your component-defined view with change detection and data binding, mapping Angular functionality to the corresponding native HTML equivalents.

\n
\n

We are working on custom elements that can be used by web apps built on other frameworks.\nA minimal, self-contained version of the Angular framework will be injected as a service to support the component's change-detection and data-binding functionality.\nFor more about the direction of development, check out this video presentation.

\n
\n

Using custom elementslink

\n

Custom elements bootstrap themselves - they start automatically when they are added to the DOM, and are automatically destroyed when removed from the DOM. Once a custom element is added to the DOM for any page, it looks and behaves like any other HTML element, and does not require any special knowledge of Angular terms or usage conventions.

\n\n

How it workslink

\n

Use the createCustomElement() function to convert a component into a class that can be registered with the browser as a custom element.\nAfter you register your configured class with the browser's custom-element registry, you can use the new element just like a built-in HTML element in content that you add directly into the DOM:

\n\n<my-popup message=\"Use Angular!\"></my-popup>\n\n

When your custom element is placed on a page, the browser creates an instance of the registered class and adds it to the DOM. The content is provided by the component's template, which uses Angular template syntax, and is rendered using the component and DOM data. Input properties in the component correspond to input attributes for the element.

\n
\n \"Custom\n
\n

Transforming components to custom elementslink

\n

Angular provides the createCustomElement() function for converting an Angular component,\ntogether with its dependencies, to a custom element. The function collects the component's\nobservable properties, along with the Angular functionality the browser needs to\ncreate and destroy instances, and to detect and respond to changes.

\n

The conversion process implements the NgElementConstructor interface, and creates a\nconstructor class that is configured to produce a self-bootstrapping instance of your component.

\n

Use the built-in customElements.define() function to register the configured constructor and its associated custom-element tag with the browser's CustomElementRegistry.\nWhen the browser encounters the tag for the registered element, it uses the constructor to create a custom-element instance.

\n
\n \"Transform\n
\n
\n

Avoid using the @Component selector as the custom-element tag name.\nThis can lead to unexpected behavior, due to Angular creating two component instances for a single DOM element:\nOne regular Angular component and a second one using the custom element.

\n
\n

Mappinglink

\n

A custom element hosts an Angular component, providing a bridge between the data and logic defined in the component and standard DOM APIs. Component properties and logic maps directly into HTML attributes and the browser's event system.

\n\n

For more information, see Web Component documentation for Creating custom events.

\n\n

Browser support for custom elementslink

\n

The recently-developed custom elements Web Platform feature is currently supported natively in a number of browsers.

\n\n\n \n \n\n\n \n \n\n\n \n \n\n\n \n \n\n\n \n \n\n\n \n \n\n
BrowserCustom Element Support
ChromeSupported natively.
Edge (Chromium-based)Supported natively.
FirefoxSupported natively.
OperaSupported natively.
SafariSupported natively.
\n

In browsers that support Custom Elements natively, the specification requires developers use ES2015 classes to define Custom Elements - developers can opt-in to this by setting the target: \"es2015\" property in their project's TypeScript configuration file. As Custom Element and ES2015 support may not be available in all browsers, developers can instead choose to use a polyfill to support older browsers and ES5 code.

\n

Use the Angular CLI to automatically set up your project with the correct polyfill:

\n\n\nng add @angular/elements --project=*your_project_name*\n\n\n\n

Example: A Popup Servicelink

\n

Previously, when you wanted to add a component to an app at runtime, you had to define a dynamic component, and then you would have to load it, attach it to an element in the DOM, and wire up all of the dependencies, change detection, and event handling, as described in Dynamic Component Loader.

\n

Using an Angular custom element makes the process much simpler and more transparent, by providing all of the infrastructure and framework automatically—all you have to do is define the kind of event handling you want. (You do still have to exclude the component from compilation, if you are not going to use it in your app.)

\n

The Popup Service example app (shown below) defines a component that you can either load dynamically or convert to a custom element.

\n\n

For comparison, the demo shows both methods. One button adds the popup using the dynamic-loading method, and the other uses the custom element. You can see that the result is the same; only the preparation is different.

\n\n\n \nimport { Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';\nimport { animate, state, style, transition, trigger } from '@angular/animations';\n\n@Component({\n selector: 'my-popup',\n template: `\n <span>Popup: {{message}}</span>\n <button (click)=\"closed.next()\">&#x2716;</button>\n `,\n animations: [\n trigger('state', [\n state('opened', style({transform: 'translateY(0%)'})),\n state('void, closed', style({transform: 'translateY(100%)', opacity: 0})),\n transition('* => *', animate('100ms ease-in')),\n ])\n ],\n styles: [`\n :host {\n position: absolute;\n bottom: 0;\n left: 0;\n right: 0;\n background: #009cff;\n height: 48px;\n padding: 16px;\n display: flex;\n justify-content: space-between;\n align-items: center;\n border-top: 1px solid black;\n font-size: 24px;\n }\n\n button {\n border-radius: 50%;\n }\n `]\n})\nexport class PopupComponent {\n @HostBinding('@state')\n state: 'opened' | 'closed' = 'closed';\n\n @Input()\n get message(): string { return this._message; }\n set message(message: string) {\n this._message = message;\n this.state = 'opened';\n }\n private _message: string;\n\n @Output()\n closed = new EventEmitter();\n}\n\n\n\n\n \n\nimport { ApplicationRef, ComponentFactoryResolver, Injectable, Injector } from '@angular/core';\nimport { NgElement, WithProperties } from '@angular/elements';\nimport { PopupComponent } from './popup.component';\n\n\n@Injectable()\nexport class PopupService {\n constructor(private injector: Injector,\n private applicationRef: ApplicationRef,\n private componentFactoryResolver: ComponentFactoryResolver) {}\n\n // Previous dynamic-loading method required you to set up infrastructure\n // before adding the popup to the DOM.\n showAsComponent(message: string) {\n // Create element\n const popup = document.createElement('popup-component');\n\n // Create the component and wire it up with the element\n const factory = this.componentFactoryResolver.resolveComponentFactory(PopupComponent);\n const popupComponentRef = factory.create(this.injector, [], popup);\n\n // Attach to the view so that the change detector knows to run\n this.applicationRef.attachView(popupComponentRef.hostView);\n\n // Listen to the close event\n popupComponentRef.instance.closed.subscribe(() => {\n document.body.removeChild(popup);\n this.applicationRef.detachView(popupComponentRef.hostView);\n });\n\n // Set the message\n popupComponentRef.instance.message = message;\n\n // Add to the DOM\n document.body.appendChild(popup);\n }\n\n // This uses the new custom-element method to add the popup to the DOM.\n showAsElement(message: string) {\n // Create element\n const popupEl: NgElement & WithProperties<PopupComponent> = document.createElement('popup-element') as any;\n\n // Listen to the close event\n popupEl.addEventListener('closed', () => document.body.removeChild(popupEl));\n\n // Set the message\n popupEl.message = message;\n\n // Add to the DOM\n document.body.appendChild(popupEl);\n }\n}\n\n\n\n\n \nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { BrowserAnimationsModule } from '@angular/platform-browser/animations';\n\nimport { AppComponent } from './app.component';\nimport { PopupComponent } from './popup.component';\nimport { PopupService } from './popup.service';\n\n// Include the `PopupService` provider,\n// but exclude `PopupComponent` from compilation,\n// because it will be added dynamically.\n\n@NgModule({\n imports: [BrowserModule, BrowserAnimationsModule],\n providers: [PopupService],\n declarations: [AppComponent, PopupComponent],\n bootstrap: [AppComponent],\n entryComponents: [PopupComponent],\n})\nexport class AppModule {\n}\n\n\n\n\n \nimport { Component, Injector } from '@angular/core';\nimport { createCustomElement } from '@angular/elements';\nimport { PopupService } from './popup.service';\nimport { PopupComponent } from './popup.component';\n\n@Component({\n selector: 'app-root',\n template: `\n <input #input value=\"Message\">\n <button (click)=\"popup.showAsComponent(input.value)\">Show as component</button>\n <button (click)=\"popup.showAsElement(input.value)\">Show as element</button>\n `,\n})\nexport class AppComponent {\n constructor(injector: Injector, public popup: PopupService) {\n // Convert `PopupComponent` to a custom element.\n const PopupElement = createCustomElement(PopupComponent, {injector});\n // Register the custom element with the browser.\n customElements.define('popup-element', PopupElement);\n }\n}\n\n\n\n\n

Typings for custom elementslink

\n

Generic DOM APIs, such as document.createElement() or document.querySelector(), return an element type that is appropriate for the specified arguments. For example, calling document.createElement('a') will return an HTMLAnchorElement, which TypeScript knows has an href property. Similarly, document.createElement('div') will return an HTMLDivElement, which TypeScript knows has no href property.

\n

When called with unknown elements, such as a custom element name (popup-element in our example), the methods will return a generic type, such as HTMLElement, since TypeScript can't infer the correct type of the returned element.

\n

Custom elements created with Angular extend NgElement (which in turn extends HTMLElement). Additionally, these custom elements will have a property for each input of the corresponding component. For example, our popup-element will have a message property of type string.

\n

There are a few options if you want to get correct types for your custom elements. Let's assume you create a my-dialog custom element based on the following component:

\n\n@Component(...)\nclass MyDialog {\n @Input() content: string;\n}\n\n

The most straightforward way to get accurate typings is to cast the return value of the relevant DOM methods to the correct type. For that, you can use the NgElement and WithProperties types (both exported from @angular/elements):

\n\nconst aDialog = document.createElement('my-dialog') as NgElement & WithProperties<{content: string}>;\naDialog.content = 'Hello, world!';\naDialog.content = 123; // <-- ERROR: TypeScript knows this should be a string.\naDialog.body = 'News'; // <-- ERROR: TypeScript knows there is no `body` property on `aDialog`.\n\n

This is a good way to quickly get TypeScript features, such as type checking and autocomplete support, for your custom element. But it can get cumbersome if you need it in several places, because you have to cast the return type on every occurrence.

\n

An alternative way, that only requires defining each custom element's type once, is augmenting the HTMLElementTagNameMap, which TypeScript uses to infer the type of a returned element based on its tag name (for DOM methods such as document.createElement(), document.querySelector(), etc.):

\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'my-dialog': NgElement & WithProperties<{content: string}>;\n 'my-other-element': NgElement & WithProperties<{foo: 'bar'}>;\n ...\n }\n}\n\n

Now, TypeScript can infer the correct type the same way it does for built-in elements:

\n\ndocument.createElement('div') //--> HTMLDivElement (built-in element)\ndocument.querySelector('foo') //--> Element (unknown element)\ndocument.createElement('my-dialog') //--> NgElement & WithProperties<{content: string}> (custom element)\ndocument.querySelector('my-other-element') //--> NgElement & WithProperties<{foo: 'bar'}> (custom element)\n\n\n \n
\n\n\n" }