My html content is generated by a markdown compiler in the backend, and what I want angular to do is to get data from server, dynamically render the content and display.
The mock markdown content may be things like this:
<h1 id="test1">Test 1<a href="#test1" title="Permanent link">¶</a></h1>
<h1 id="test2">Test 2<a href="#test2" title="Permanent link">¶</a></h1>
In foo.component.html
, I use innerHTML
attribute to display contents, like this:
<div [innerHTML]="mock_markdown_content"></div>
Now, the link on those anchor tags will become things like localhost:4200/#test1
, and cannot navigate to the location of these elements as expected.
Since the html content is generated by markdown compiler, I don't want to change the html content itself.
Actually, the url link that this foo.component
will be displayed in browser is something like localhost:4200/post/post-title
, so the expected url of these anchor tags are localhost:4200/post/post-title#test1
.
I found that the official guide of Angular can do what exactly I want, like this: https://angular.io/guide/router#overview (correctly display the url and can navigate to the element position). How do they implement this?
Test 1¶
`, and when I move onto the link in Angular's rendered html, it shows `localhost:4200/#test1` – funkid May 18 '19 at 14:55