Having trouble mixing some version of JavaScript in my Angular Typescript file.
In the code below, in ngOnInit, the function globalNavigationHandler is not recongnized and I cannot use this.globalNavigationHandler because this has a different meaning which I need to keep.
I have an eventlistener that is not an arrow function because I need the THIS rebound.
However, I do not know how to properly get the function to recognize the other function that I have called globalnavigationHandler. I cannot put globalNavigationHandler inline because it needs the router which is injected into the constructor and has a different meaning of this.
globalNavigationHandler = function(path) {
this.router.navigate([path]);
}
ngOnInit() {
var nav = document.getElementById("uhf-c-nav");
var anchors = nav.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].addEventListener(
"click",
function(event) {
event.preventDefault();
console.log("this is the element the event is bound to: " + this.id);
console.log("the event target is the clicked element: " + event);
console.log("this: " + this);
console.log("this.pathname: " + this.pathname);
globalNavigationHandler(this.pathname);
},
false
);
}
}
Thanks