|
| 1 | +# Elements |
| 2 | + |
| 3 | +## Release Status |
| 4 | + |
| 5 | +**Angular Labs Project** - experimental and unstable. **Breaking Changes Possible** |
| 6 | + |
| 7 | +Targeted to land in the [6.x release cycle](https://github.com/angular/angular/blob/master/docs/RELEASE_SCHEDULE.md) of Angular - subject to change |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +Elements provides an API that allows developers to register Angular Components as Custom Elements |
| 12 | +("Web Components"), and bridges the built-in DOM API to Angular's component interface and change |
| 13 | +detection APIs. |
| 14 | + |
| 15 | +```ts |
| 16 | +//hello-world.ts |
| 17 | +import { Component, Input, NgModule } from '@angular/core'; |
| 18 | +import { createNgElementConstructor, getConfigFromComponentFactory } from '@angular/elements'; |
| 19 | + |
| 20 | +@Component({ |
| 21 | + selector: 'hello-world', |
| 22 | + template: `<h1>Hello {{name}}</h1>` |
| 23 | +}) |
| 24 | +export class HelloWorld { |
| 25 | + @Input() name: string = 'World!'; |
| 26 | +} |
| 27 | + |
| 28 | +@NgModule({ |
| 29 | + declarations: [ HelloWorld ], |
| 30 | + entryComponents: [ HelloWorld ] |
| 31 | +}) |
| 32 | +export class HelloWorldModule {} |
| 33 | +``` |
| 34 | + |
| 35 | +```ts |
| 36 | +//app.component.ts |
| 37 | +import { Component, NgModuleRef } from '@angular/core'; |
| 38 | +import { createNgElementConstructor } from '@angular/elements'; |
| 39 | + |
| 40 | +import { HelloWorld } from './hello-world.ngfactory'; |
| 41 | + |
| 42 | +@Component({ |
| 43 | + selector: 'app-root', |
| 44 | + templateUrl: './app.component.html', |
| 45 | + styleUrls: ['./app.component.css'] |
| 46 | +}) |
| 47 | +export class AppComponent { |
| 48 | + constructor(ngModuleRef: NgModuleRef) { |
| 49 | + const ngElementConfig = getConfigFromComponentFactory(HelloWorld, injector); |
| 50 | + const NgElementConstructor = createNgElementConstructor(ngElementConfig); |
| 51 | + customElements.register('hello-world', NgElementConstructor); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +``` |
| 56 | +Once registered, these components can be used just like built-in HTML elements, because they *are* |
| 57 | +HTML Elements! |
| 58 | + |
| 59 | +They can be used in any HTML page: |
| 60 | + |
| 61 | +```html |
| 62 | +<hello-world name="Angular"></hello-world> |
| 63 | +<hello-world name="Typescript"></hello-world> |
| 64 | +``` |
| 65 | + |
| 66 | +Custom Elements are "self-bootstrapping" - they are automatically started when they are added to the |
| 67 | +DOM, and automatically destroyed when removed from the DOM. |
0 commit comments