I have an angular 5 component that needs to open a link in new tab, I tried the following:
page link
Try this:
window.open(this.url+'/create-account')
No need to use '_blank'
. window.open
by default opens a link in a new tab.
just use the full url as href like this:
<a href="https://www.example.com/" target="_blank">page link</a>
Some browser may block popup created by window.open(url, "_blank");
.
An alternative is to create a link and click on it.
...
constructor(@Inject(DOCUMENT) private document: Document) {}
...
openNewWindow(): void {
const link = this.document.createElement('a');
link.target = '_blank';
link.href = 'http://www.your-url.com';
link.click();
link.remove();
}
you can try binding property whit route
in your component.ts
user:any = 'linkABC'
;
in your component.html
<a target="_blank" href="yourtab/{{user}}">new tab </a>
<a [routerLink]="" (click)="openSite(SiteUrl)">{{SiteUrl}}</a>
and in your Component.ts
openSite(siteUrl) {
window.open("//" + siteUrl, '_blank');
}
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');
}