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. - Angular.io
Shipping Angular Components
as Web Components
is easy as pie. You just have to add the @angular/elements
package and use the createCustomElement()
API.
ng add @angular/elements
Next up is to create an actual component. It's a subscription component and somewhat configurable. Users can enter their email addresses and submit to get registered with a subscription plan.
The following goes into the script file i.e. the component file,
And the markup is simple as this,
Singleton service class is doing a mock delay request and returning a string containing a notification. You can use the HttpClient
to make a post request to your preferred server. Of course, the URL is configurable here, and it can be set by the consumer of the component.
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { delay } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class SubscriptionService {
constructor() {}
// Create a POST request using the url and register email in the backend
register(url: string, email: string): Observable<string | undefined> {
return of(
`Please check your email (${email}) and confirm subscription`
).pipe(delay(500));
}
}
The bridging of Angular component to web component happens in the app.module.ts
.
Things to notice are:
- How the
createCustomeElement
API is used to create a custom element. customElements.define()
is used to register the element to browser'sCustomElementRegistry
.- Components are registered using the
entryComponents
option so that they can be dynamically loaded into the view ngDoBootstrap
is used for manual bootstrapping of the application instead of using thebootstrap
array.
Transforming a component to a custom element makes all of the required Angular infrastructure available to the browser - Angular.io
You can do a production build of the app and get the minified styles and scripts from the dist
folder. You can reference these files in any other HTML project and start using the subscriber-element
.
For quick testing, you can also use the index.html
file that resides in your Angular app. The following markup shows how to use the element in plain HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>NgPlayground</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<div class="section">
<div class="container">
<h1 class="title">Subscription Form</h1>
<h2 class="subtitle">
A web component created with <code>@angular/elements</code> project
</h2>
<subscriber-element
url="https://api.lorem.com/subscribe"
></subscriber-element>
</div>
</div>
<div class="section">
<div class="container">
<h2 class="subtitle">Console</h2>
<pre></pre>
</div>
</div>
<script>
var el = document.querySelector('subscriber-element');
el.addEventListener('subscribed', evt => {
const pre = document.querySelector('pre');
pre.innerText += `Log: ${evt.detail}\n`;
});
</script>
</body>
</html>
Repository:
https://github.com/fiyazbinhasan/ng-playground/tree/ng-web-components
Links:
https://angular.io/api/core/NgModule#entryComponents
https://angular.io/api/elements/NgElementConfig
Comments