Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a link in new tab using angular?

People also ask

How do I make links open in a new tab?

The first method requires a keyboard and a mouse or trackpad. Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.

How do you get a link to open in a new tab with the value for the target attribute?

If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.

How do I open a link in a new tab without switching?

1) select "open link in new background tab" from the context menu (second choice), 2) use middle-click on the link with your mouse; 3) use Ctrl+leftclick on the link with your mouse, 4) designate a mouse gesture to open a background tab when you perform it over a link.


Use window.open(). It's pretty straightforward !

In your component.html file-

<a (click)="goToLink('www.example.com')">page link</a>

In your component.ts file-

goToLink(url: string){
    window.open(url, "_blank");
}

just use the full url as href like this:

<a href="https://www.example.com/" target="_blank">page link</a>

Just add target="_blank" to the

<a mat-raised-button target="_blank" [routerLink]="['/find-post/post', post.postID]"
    class="theme-btn bg-grey white-text mx-2 mb-2">
    Open in New Window
</a>

I have just discovered an alternative way of opening a new tab with the Router.

On your template,

<a (click)="openNewTab()" >page link</a>

And on your component.ts, you can use serializeUrl to convert the route into a string, which can be used with window.open()

openNewTab() {
  const url = this.router.serializeUrl(
    this.router.createUrlTree(['/example'])
  );

  window.open(url, '_blank');
}