constructor() {
document.addEventListener('someEvent', function(event) {
console.log(event.data);
this.test(); //throws error - this.test() is undefined
});
}
If I move it to ngOnInit
, it works fine:
ngOnInit() {
this.test(); //works ok
}
The function is just a method on the component, defined after the constructor:
public test(){
console.log('TEST');
}
My end goal is to run the test()
method of my component every time someEvent
event fires on the global window (the event comes from raw javascript, i.e. document.dispatchEvent(event, {'detail': evntPayload});
How do I accomplish this?