How to use routerLink with innerHTML

纵饮孤独 提交于 2020-07-22 06:01:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!