Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 routerLink on a new tab

Tags:

angular

I'm trying to open a routerlink on a new tab instead of on the current page but regular html attributes like target _blank are not working.

<span routerLink="/custompage/{{city.id}}"  class="badge badge-primary badge-pill">open</span>
like image 734
John Avatar asked Feb 01 '19 16:02

John


People also ask

Can I use routerLink on Div?

Yes it can be attached to div tag, your route is probably wrong try add / in front of route.

Can we use href instead of routerLink?

routerLink is the attribute provided by angular to navigate to different components without reloading the page. Major difference between both is that href kills the state of the current page where routerLink doesnt lose the state of the page.

Can we add routerLink to button?

Using Router linksAfter Angular v4 we can directly add a routerLink attribute on the anchor tag or button. Consider the following template, where routerLink attribute added to the anchor tag. The routerLink directive on the anchor tags give the router control over those elements.


5 Answers

Here's an alternate way of doing within a component.

openCityInNewWindow(city) {
  // Converts the route into a string that can be used 
  // with the window.open() function
  const url = this.router.serializeUrl(
    this.router.createUrlTree([`/custompage/${city.id}`])
  );

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

Html will look something like

<ul *ngFor="let city of cities">
  <li (click)="openCityInNewWindow(city.id)">{{city.name}}</li>
</ul>
like image 89
convrge Avatar answered Nov 15 '22 19:11

convrge


There is a newer way of opening new tabs using routerLink, but in my opinion, this is still a simpler option, as both methods still require to make use of window.open().

On your component.ts,

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

or this on your component.html

<a href="www.domain.com/custompage/{{city.id}}" target="_blank">

Another valid alternative is to write your own custom directives. Check it out over here, that person has already written a sample implementation of it.

like image 22
wentjun Avatar answered Nov 15 '22 19:11

wentjun


you can add target="_blank" to your link

it works for me:

 <a routerLink="/News" routerLinkActive="active" target="_blank" rel="bookmark"></a>
like image 20
Paniz Rostamkalaei Avatar answered Nov 15 '22 19:11

Paniz Rostamkalaei


I encountered the exact same issue and i think i've found the best of both worlds.

Change your DIV, SPAN into A tags. Add the href link but keep the routerLink.

<a routerLink="/custompage/{{city.id}}"  href="/custompage/{{city.id}}" class="badge badge-primary badge-pill">open</a>

How it should work now :

  • Left click -> go to corresponding route / page without a new reload ;
  • Right click -> Open in new tab / window options and would naturally load a new page when using this navigation
like image 42
Tim Meehan Avatar answered Nov 15 '22 19:11

Tim Meehan


I find the solution and fixed this as below

 <a [routerLink]="['/unauth/profile',data.userId]" target="_blank" class="FullViewLink">FullView<i class="fa fa-arrow-right"></i></a>

You can targe="_blank" in routerLink. It is working fine for me.

like image 40
Manoj Tyagi Avatar answered Nov 15 '22 19:11

Manoj Tyagi