Update please, check the @K-coding's response, it's a better aproach than mine!
A very poor another aproach:
Angular not compile, so if you add an .html you can not use "Angular" to control. You need make in JavaScript.
The problem when mixing JavaScript and Angular is that we can not trust in the name of the functions. So we need dispatch a CustomEvent and subscribe to it
Well, we are going to create a function that gets all the "link" inside a div I call wrapper and use a template reference variable
<div #wrapper>
<div [innerHTML]="link"></div>
</div>
createLinks()
{
//get the "links" inside the "wrapper"
const links = this.wrapper.nativeElement.getElementsByTagName('a');
//with each link
for (var i = 0; i < links.length; i++) {
//we change the color only to check if work
links[i].style.color = 'red';
//we override the event click
links[i].onclick = (event) => {
//prevent default
event.preventDefault();
//get the "target" using getAttribute('href')
const target = (event.target as HTMLElement).getAttribute('href');
//dispatch a custom event in "wrapper"
this.wrapper.nativeElement.dispatchEvent(
new CustomEvent<string>('route', { detail: target })
);
};
}
}
//and in ngOnInit, we subscribe to the custom event
ngOnInit()
{
fromEvent(this.wrapper.nativeElement, 'route').subscribe((res: any) => {
this.router.navigate([res.detail]);
});
}
}
A simple stackblitz
NOTE: See in the stackblitz that I call to the function "createLink" "after" all is painted. In the stackblitz I use a setTimeout to give time to Angular to paint -in this case I coud use ngAfterViewInit, but in case you create the D3 tree you need call it after paint the 3D