Build a dynamic URL on anchor tag click - Angular 5

六眼飞鱼酱① 提交于 2019-12-12 12:27:54

问题


I have a table of several anchor tags. I want to create an external URL dynamically on clicking each anchor tag. I tried using [routerLink] but it is getting the base URL appended. Is there any angular way of doing it ? Any help is much appreciated.

html

<ng-container matColumnDef="invoiceNo">
          <mat-header-cell *matHeaderCellDef > Invoice # </mat-header-cell>
          <mat-cell *matCellDef="let invoice"> 
            <a [routerLink]="getInvoiceUrl()"  target="_blank">
              {{invoice.invoiceNumber}}
            </a>
          </mat-cell>
        </ng-container>

ts

 getInvoiceUrl(){

return "www.google.com";

}


回答1:


yes, just use href:

<a [href]="getInvoiceUrl()">

Make sure you include http: else it will include the domain.

so:

return "http://www.google.com";



回答2:


You need to configure your own routes if you are using routerLink, in your case just use [href]

<a href="{{getInvoiceUrl()}}">Link</a>



回答3:


Could you use something other than an anchor tag - like perhaps have:

<span (click)="takeMeToDynamicUrl()">link text</span>

And then your method can create the URL and use any numb er of available methods to take you to the URL e.g.

document.location.href = 'https://dynamicallycreatedurl.com';


来源:https://stackoverflow.com/questions/51640982/build-a-dynamic-url-on-anchor-tag-click-angular-5

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