[英]how to use routerLink instead of href in anchor tag in 'innerHTML' in angular2+?
角度 2+
在 .ts
this.htmlText = 'The validation window for <a href=\"http://localhost/nomination/">Innovator Mind</a> nominations is open now. Last date to review is 2020-Jan-25';
在 HTML 中,
<div [innerHTML]="htmlText "></div>
在 .ts
this.htmlText = 'The validation window for <a routerLink=\"http://localhost/nomination/">Innovator Mind</a> nominations is open now. Last date to review is 2020-Jan-25';
在 HTML 中,
<div [innerHTML]="htmlText"></div>
那么如何在不重新加載頁面的情況下實現頁面的路由呢?
在構造函數中添加。
private router: Router
使用點擊事件通過 ts 更改 url ->
.ts:
html='<a value="myRoute">link</a>';
changeRoute(value){
let myRoute=value.target.attributes.value.value;
if(myRoute){
this.router.navigate([ myRoute ]);
}
}
.html :
<div (click)='changeRoute($event)' [innerHTML]="html"> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.