简体   繁体   English

如何在angular2+的'innerHTML'中的锚标记中使用routerLink而不是href?

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

In angular 2+角度 2+

  1. If I'm using href to redirect then it reloads the whole page .如果我使用href重定向,则它会重新加载整个页面

In .ts在 .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';

in HTML ,在 HTML 中

<div [innerHTML]="htmlText "></div>
  1. If I'm using routerlink to redirect then routerlink does not get render in html.如果我使用routerlink进行重定向,则routerlink 不会在 html 中呈现

In .ts在 .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';

in HTML ,在 HTML 中

<div [innerHTML]="htmlText"></div>

So how to achieve the route the page without reloading the page?那么如何在不重新加载页面的情况下实现页面的路由呢?

add in constructor.在构造函数中添加。

private router: Router

Use click event for change url by ts ->使用点击事件通过 ts 更改 url ->

.ts: .ts:

   html='<a value="myRoute">link</a>';

   changeRoute(value){
     let myRoute=value.target.attributes.value.value;
      if(myRoute){
       this.router.navigate([ myRoute ]);
      }
   }

.html : .html :

<div (click)='changeRoute($event)' [innerHTML]="html"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM