问题
I am working on angular6. I just want to create the dynamic route by using the innerHTML.I tried the following code but it is not working. In the component file, I wrote the following code inside the constructor
this.anchor = "<a routerLink='login'>Dynamic Login</a>"
In the template file, I wrote the following code
<div [innerHtml]="anchor"></div>
回答1:
this.anchor = "<a routerLink='login'>Dynamic Login</a>"
<div [innerHtml]="anchor"></div>
Above codes won't cause Angular to process anything Angular-specific in anchor
.
Angular replaces Angular specific markup at build time with generated code. Markup added at runtime won't be processed by Angular.
To add HTML that contains Angular-specific markup (property or value binding, components, directives, pipes, ...) it is required to add the dynamic module and compile components at runtime. This answer provides more details How can I use/create a dynamic template to compile dynamic Component with Angular 2.0?
回答2:
I had the same issue. I just replaced the <a routerLink="/something">Something</a>
to
<a href="#/something">Something</a>
And it works just fine
来源:https://stackoverflow.com/questions/55350647/how-to-use-routerlink-with-innerhtml