How to open a link in new tab using angular?

HtmlAngularRoutesAnchor

Html Problem Overview


I have an angular 5 component that needs to open a link in new tab, I tried the following:

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

when I open the link, the application gets slow and opens a route like:

localhost:4200/www.example.com

My question is: What is the correct way to do this in angular?

Html Solutions


Solution 1 - Html

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");
}

Solution 2 - Html

just use the full url as href like this:

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

Solution 3 - Html

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

Solution 4 - Html

Solution 5 - Html

<a [routerLink]="" (click)="openSite(SiteUrl)">{{SiteUrl}}</a>

and in your Component.ts

openSite(siteUrl) {
   window.open("//" + siteUrl, '_blank');
}

Solution 6 - Html

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();
  }

Solution 7 - Html

In the app-routing.modules.ts file:

{
    path: 'hero/:id', component: HeroComponent
}

In the component.html file:

target="_blank" [routerLink]="['/hero', '/sachin']"

Solution 8 - Html

Try this:

 window.open(this.url+'/create-account')

No need to use '_blank'. window.open by default opens a link in a new tab.

Solution 9 - Html

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>

Solution 10 - Html

u can add https in your code and it works for me

goToLink(url: string) {
    window.open('https://' + url, "_blank");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlejoDevView Question on Stackoverflow
Solution 1 - HtmlHarun Or RashidView Answer on Stackoverflow
Solution 2 - HtmlzhiminView Answer on Stackoverflow
Solution 3 - HtmlwentjunView Answer on Stackoverflow
Solution 4 - HtmlC.IkongoView Answer on Stackoverflow
Solution 5 - HtmlJohn Anthony RomualdoView Answer on Stackoverflow
Solution 6 - HtmlMorlo MbakopView Answer on Stackoverflow
Solution 7 - HtmlFeminaView Answer on Stackoverflow
Solution 8 - HtmlDINESH AdhikariView Answer on Stackoverflow
Solution 9 - HtmljdcreativemakerView Answer on Stackoverflow
Solution 10 - Htmlhaga gintingView Answer on Stackoverflow