I have a simple Angular Web Component that fires an event which is caught just fine when captured from another Angular container component. But when I try to capture the event from JavaScript, I get nothing, even though the component renders fine and fires the event fine.
Here is my raw JavaScript that does not work:
document.addEventListener("DOMContentLoaded", (event) => {
const h = document.querySelector('frvp-header');
h.addEventListener('darkChange', (e) => {
// never gets here
const dark = e.detail;
console.log('dark', dark);
});
});
The selector is <frvp-header></frvp-header>
And in the Angular component I can confirm the following emitter is fired:
@Input() dark = false;
@Output darkChange = new EventEmitter<boolean>();
...
toggleDark() {
this.dark = !this.dark;
this.darkChange.emit(this.dark);
}
I have tried using different ViewEncapsulation.None and Emulated. I have tried to fire the event manually from the console using the following JavaScript and the event fires fine then:
let ev = new Event('darkChange');
let hd = document.querySelector('frvp-header');
hd.dispatchEvent(ev);
I have tried to change return tyype to a string: EventEmiter<string>()
, and call it with: this.darkChange.emit(this.dark.toString())
, but still no go.
I imagine it is something simple that I haven't tried yet.
So the question is, how do I capture the Angular Web Component event with raw JavaScript?