ANGULAR DAY 2018
DEVELOPER & CONSULTANT
MICHELE STIEVEN
Social
Web components
A set of APIs to create your own
custom elements
Specifications
•HTML Templates
•HTML Imports
•Shadow Dom
•Custom Elements
Templates
<template>
<p>You don’t see me!</p>
<p>Call me later!</p>
</template>
Imports
<link rel=“import” href=“hello.html”>
Shadow dom
<my-component>
#shadow-root
<style>.a { color: red }</style>
<p class=“a”>I’m red!</p>
</my-component>
Custom elements
EXTEND THE BROWSER VOCABULARY
DIV
P
H1
MY-COMPONENT
class MyComponent extends HTMLElement { … }
customElements.define(‘my-comp’, MyComponent)
Not that easy…
class MyComponent extends HTMLElement {
}
Attributes
static get observedAttributes() {
return [‘label’, ‘value’];
}
attributeChangedCallback(name, old, new) {
switch(name) {
// Do something
}
}
class MyComponent extends HTMLElement {
}
Properties
get label() {
return this.getAttribute(‘label’);
}
set label(value) {
this.setAttribute(‘label’, value);
}
let myComp = document.querySelector(‘my-comp’);
myComp.label = “Hey!”
class MyComponent extends HTMLElement {
}
Events
emitValueChange {
let myEvent = new CustomEvent(‘value-change’, {
value: this.value
})
this.dispatchEvent(myEvent);
}
let myComp = document.querySelector(‘my-comp’);
myComp.addEventListener(‘value-change’, e => … );
class MyComponent extends HTMLElement {
}
constructor() { super() }
connectedCallback() { … }
disconnectedCallback() { … }
attributeChangedCallback() { … }
Reactions
…or lifecycle hooks ?
From the outside…
• Attributes
• Properties
• Events
• Methods
They work just like native elements!
they Work Everywhere
You can already use Custom Elements
inside your Angular app!
You cannot use Angular Components
outside of Angular.
Situation
ANGULAR

ELEMENTS
You create an Angular component.
Angular wraps it into a Custom Element!
Component
Element
Attributes ListenersProperties
Component
<my-component>
Angular VanillaReact
@Component({ … })
export class MyComponent {}
How?
import { createCustomElement } from ‘@angular/elements’;
customElements.define(‘my-comp’, MyElement);
const MyElement = createCustomElement(MyComponent, {
injector: someInjector
});
DEMO
ANGULAR 7
TO BE CONTINUED IN
• IVY will reduce Angular size
• IVY will solve conflicts between versions
• The CLI will make the process smoother
• Browsers will have better support for Custom

Elements v1 (no polyfills required)
thanks!
CODE
https://goo.gl/LjVPBs

AngularDay 2018 - Angular Elements