I have been looking around for a way to enable standard a[href] links to act like routerLinks when loaded dynamically into [innerHTML]. It doesn't seem to be something that works as standard and I couldn't find anything that did what I needed.
I have a working solution but wondered if anybody knows of any better ways to do this or any potential issues I might run into doing it this way.
I am using jQuery from inside my app-routing.module.ts so I declare the variable:
declare var $:any;
I then find all anchors with the href attribute that do not also have the routerlink attribute. I can then grab the pathname and tell the router to navigate to it on click.
$(document).on('click', `a[href]:not("a[routerlink]"):not("a[href^='mailto:']"):not("a[href^='tel:']")`, function(e){
if(location.hostname === this.hostname || !this.hostname.length){
e.preventDefault();
router.navigate([this.pathname]);
}
});
This seems to do the job but I thought there must be a more 'Angular' way of doing this...