4

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
  • Hi, JavaLearner. Have you find the solution? I have similar problem. Commented May 21, 2019 at 17:09

4 Answers 4

3

yes, just use href:

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

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

so:

return "http://www.google.com";
Sign up to request clarification or add additional context in comments.

1 Comment

One issue is I want to create the link dynamically on. So when I add the function call in href.it is giving me bad response error. When I try to add it in (click) event again that is error .....Any suggestions.<a (click)="{{getInvoiceUrl(invoice.filePath)}}" target="_blank">
2

public link="public"

use this in a tag --> [href]=link> {{ link }}

It will redirect to www.localhost:4200/public

Comments

1

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

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

1 Comment

One issue is I want to create the link dynamically on. So when I add the function call in href.it is giving me bad response error. When I try to add it in (click) event again that is error .....Any suggestions.<a (click)="{{getInvoiceUrl(invoice.filePath)}}" target="_blank">
0

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';

1 Comment

that way the dom node lost "<a>" styling and behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.