I'm building a few WebComponent
that I'll be using across different libraries/frameworks, but mostly with plain vanilla JavaScript
. I'm considering using Angular Elements for this, and built a sample one following few documents.
It works fine, with all natural Angular goodies built in. But, what I'm struggling with is, reading data out of the web component from plain JavaScript
, something like var result = myComponent.value
.
- Note that this data I'm reading is not primitive, or a single value. It's like an object with many details from a complex
WebComponent
- I'd want something like a
readonly property
- Standard
Angular
documents for components always use@Output()
with anEventEmitter
for this purpose. But since I'll be using the component asWebComponent
outsideAngular
world, I cannot (and don't want to) use that approach. Also, I do not want to notify on each change of the value, but just read the value when needed
If I'm writing a standard WebComponent
in plain JavaScript
, I can do this
class MyComponent extends HTMLElement {
constructor() {
super();
this.name = 'Guest';
this.count = 0;
}
// more code...
// THIS result PROPERTY IS USED TO READ DATA FROM THE WEB COMPONENT
get result() {
return { name: this.name, count: this.count }; // <<== to read data
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
styles....
</style>
<h1>MyComponent</h1>
More HTML...
`;
}
}
customElements.define('my-component', MyComponent);
// Then I can use <my-component></my-component> in my application HTML
// And in the JavaScript code, I can read data from my-component as
const result = document.querySelector('my-component').result;
// which would be something like { name: 'Guest', count: 0 }
Is there a way to do this in AngularElements
?