簡體   English   中英

如何在angular2+的'innerHTML'中的錨標記中使用routerLink而不是href?

[英]how to use routerLink instead of href in anchor tag in 'innerHTML' in angular2+?

角度 2+

  1. 如果我使用href重定向,則它會重新加載整個頁面

在 .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>
  1. 如果我使用routerlink進行重定向,則routerlink 不會在 html 中呈現

在 .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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM