Pankaj Parkar
Indian | 30 | Ex MVP | Angular GDE
ng-India
#ngIndia
www.ng-ind.com
Feb 24, 2018
Pankaj Parkar
@pankajparkar
Senior Software Engineer
TSS Consultancy PVT LTD
Angular is ideal for building complete applications
Rob Wormald, Angular Team
But it becomes challenging for scenarios that don't fit in Single Page Application Model
Angular components are tough to use outside Angular
Rob Wormald, Angular Team
<template>
<h1>Hello World!</h1>
</template>
We have similar technique, that used inside `ng-template`
They can be used at runtime
<my-app>
<#shadow-root>
<h1 class=”title”></h1>
</shadow-root>
</my-app>
We can do this for component using encapsulation set to Native.
<head>
<link
rel="import"
href="my-widget.html" />
</head>
This spec is deprecated, this is not going to implement by browsers.
Html Imports
<body>
<my-navbar></my-navbar>
<div>
<my-sidebar></my-sidebar>
<my-main-content></my-main-content>
</div>
<my-footer></my-footer>
</body>
Html Imports
class MyDatePicker extends HTMLElement {}
window.customElements
.define(‘my-datepicker’, MyDatePicker);
class MyDatePicker extends HTMLElement {
static observedAttributes = [‘current-date’];
attributeChangedCallback(oldV, newV, key){
//update the DOM somehow
}
}
<my-datepicker
current-date=”10/10/2017”>
</my-datepicker>
const picker = document.querySelector(‘fancy-datepicker’);
picker.setAttribute(‘current-date’, new Date().toString());
class MyDatePicker extends HTMLElement {
set currentDate(value){
//update the DOM somehow
}
get currentDate(){ … }
}
const myDatePicker = document.querySelector(‘my-datepicker’);
myDatePicker.currentDate = new Date();
const picker = document.querySelector(‘my-datepicker’);
const onDateChange = (date) => console.log(date);
picker.addEventListener(‘date-change’, onDateChange);
class MyDatePicker extends HTMLElement {
emitDateChange(){
let dateChangeEv =
new CustomEvent(‘date-change’,{details});
this.dispatchEvent(dateChangeEv);
}
}
const myDatePicker = document.querySelector(‘my-datepicker’);
myDatePicker.currentDate = new Date();
class FancyDatePicker extends HTMLElement {
connectedCallback(){ … }
disconnectedCallback(){ … }
attributeChangedCallback(oldV, newV, key){ … }
adoptedCallback(){ … }
}
Web Component works out of the box in Angular, it supports by design
<my-angular-app>
<my-input
[attr.foo]=”someString”
[someProp]=”someProp”
(someEvent)=”doStuff()”>
</my-input>
</my-angular-app>
Source: http://pascalprecht.github.io/slides/angular-elements/#/14
@HostBindings
@Input
@Output
@Lifecycle Hooks
Attributes
Properties
CustomEvent
Reactions
Angular Component packaged as web component
SkateJS
// app.module.ts
import { HelloWorldComponent } from './hello-world';
export const CEComponents = [HelloWorldComponent];
@NgModule({
imports: [BrowserModule],
declarations: CEComponents,
//all custom-element should be here
entryComponents: CEComponents
})
export class CustomElementsModule {
ngDoBootstrap() {} // required in bootstrap module
}
// main.ts
import { registerAsCustomElements } from '@angular/elements';
import { CEComponents, CustomElementsModule } from './app';
registerAsCustomElements(CEComponents, () => {
return platformBrowserDynamic()
.bootstrapModule(CustomElementsModule);
}).then(_ => {
// application code goes here
}).catch(onError);
<html>
<head>
...
...
<script src="my-customelement.bundle.js"></script>
...
</head>
<body>
...
<my-ng-component></my-ng-component>
...
</body>
</html>
<html>
<head>
...
...
<script src="angular-mini.js"></script>
<script src="my-custom-element.bundles.js"></script>
...
</head>
<body>
...
<my-ng-component></my-ng-component>
...
</body>
</html>
Jquery dialog modal example
<html>
<head>
...
...
<script src="jquery.js"></script>
<script src="jquery.datepicker.js"></script>
...
</head>
<body>
...
<input id="my-datepicker" />
<script type="javascript">
$('#my-datepicker').datePicker({
format: 'dd-MM-yyyy'
})
</script>
...
</body>
</html>
https://blog.angular.io/the-angular-team-at-angularmix-2d56fd7fde65
https://github.com/angular/angular/issues/21706
http://slides.com/andreiantal/ng-europe_angular-elements#/33
http://slides.com/andreiantal/ng-europe_angular-elements#/3
https://github.com/andrei-antal/ng-europe-demo-angular-elements (Thanks Andrei Antal)
https://github.com/pankajparkar/ng-india-angular-elements-demo
https://github.com/pankajparkar/ng-india-vueapp
https://github.com/pankajparkar/ng-india-reactapp
https://medium.com/@ezekizibzibadze/angular-and-web-components-a-k-a-angular-elements-e009b057cd75
By Pankaj Parkar