thomasgassmann.net@gassmannT
Client-Architekturen mit Angular Elements
Thomas Gassmann
Agenda
• Intro Angular Elements
• Web Components
• Getting started with Angular Elements
• Outlook
Angular Elements
«Angular is ideal for building complete applications, and
our tooling, documentation and infrastructure are
primarily aimed at this case.»
Rob Wormald, Angular Team
Platform
Dependency
Injection
Decorators Zones
Compile Binding Render
Material Mobile Universal
CLI
Language
Service
Augury
ngUpdate
Router
Animation
i18n
«[…] but it’s quite challenging to use in
scenarios that don’t fit that specific Singe Page Application
model.»
Rob Wormald, Angular Team
Use Cases
• Enhancing existing HTML Pages
• Content Management Systems integration
• Use components in other environments or frameworks
• Microfrontends
• Migrating Legacy Apps
Web Components
Web Components
Web Components are a set of features added by the W3C
HTML Template
Shadow DOM
HTML Imports
Custom Elements
Custom Elements
Custom elements share the same API surface as native
DOM objects:
• Attributes
• Properties
• Methods
• Events
Create and Define a Custom Element
Reactions
Attributes
<world-cup-statistics country="sui"></world-cup-statistics>
Properties
let games = document.querySelector('world-cup-statistics');
games.country = 'ger';
Custom Event
let games = document.querySelector('world-cup-statistics');
games.addEventListener('country-change', event => { ... });
Custom Elements in Angular
=> Angular has been designed for this
Enter Angular Elements
Transforming
Angular Component
to Custom Element
Enter Angular Elements
Provides a bridge from angular concepts to web
components.
@HostBinding() => Attributes
@Input() => Properties
@Output() => CustomEvents
Lifecycle Hooks => Reactions
Getting started
Platform
Dependency
Injection
Decorators Zones
Compile Binding Render
Material Mobile Universal
CLI
Language
Service
Augury
ngUpdate
Router
Animation
i18n
• Create a new Angular CLI project
ng new angularElements
First Steps
• Update Angular CLI
• Add support for angular elements
ng add @angular/elements
• Generate new component
ng g c <NAME> -v ShadowDom
Component
Module
Module
<world-cup-statistics></world-cup-statistics>
Polyfills
• Install NPM Package
npm install @webcomponents/custom-elements –-save
First Steps
• Add import to polyfills.ts
import '@webcomponents/custom-elements/custom
-elements.min';
import '@webcomponents/custom-elements/src/native-shim';
Add Custom Element dynamically
DEMO
Dependency Injection
Plattform Injector
(Renderer)
Module Injector
(Services)
Component Injector
(ElementRef)
DI in Angular Elements
Plattform Injector
(Renderer)
Module Injector
(Services)
Element Injector
(ElementRef)
Element Injector
(ElementRef)
Module
Shadow DOM
DEMO
Angular Elements in V8
• It is just the beginning
• Size is too big for shipping in non Angular projects
• Will be much better with Ivy (V9)
• Will be much easier with V9+
• Browser Support.
Outlook
How to use it today in non
Angular Projects?
• Install package ngx-build-plus
ng add ngx-build-plus
Optimize and combine bundle
• Add script command
"build:element": "ng build --prod --output-hashing=none --
single-bundle --keep-polyfills"
• Run Command
npm run build:element
Output
DEMO
Thomas Gassmann
• Web development using .NET, Typescript and
Angular
• Trainer, Speaker
@gassmannT thomasgassmann.net
Ressources
• github.com/gassmannT/ngElement
• thomasgassmann.net
• swissangular.com
• m.trivadis.com/angular
TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas Gassmann - Trivadis

TechEvent 2019: Nachhaltige Client-Architekturen mit Angular Elements; Thomas Gassmann - Trivadis