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>
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
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.
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.
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>
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.
you can add target="_blank" to your link
it works for me:
<a routerLink="/News" routerLinkActive="active" target="_blank" rel="bookmark"></a>
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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With