206

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?

Liam
  • 27,717
  • 28
  • 128
  • 190
AlejoDev
  • 4,345
  • 9
  • 36
  • 67

10 Answers10

282

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

In your component.html file:

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

You may have to add the http prefix if it doesn't redirect correctly:

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

In your component.ts file:

goToLink(url: string){
    window.open(url, "_blank");
}
Powkachu
  • 2,170
  • 2
  • 26
  • 36
Harun Or Rashid
  • 5,589
  • 1
  • 19
  • 21
  • Can someone point out a way of doing this where the string within the goToLink function isn't a 'magic string'? For instance, through a variable? – Highspeed Jul 07 '19 at 23:05
  • @BrianAllanWest I would just set a variable in the corresponding component file and leave the parameter out in the html. If this doesn't work, just bind it to the template using the default binding property [ ] and then you can run whatever logic you want on that. .html goToLink() .ts goToLink() { window.open(Your Variable Here, "_blank") } – thenolin Jul 29 '19 at 16:42
  • How can I let it be in a small popup window, and listen to changes in its URL. – Mostafa Al Belliehy Nov 03 '19 at 08:32
  • I would add a `href=''`, so it stills looks clickable – Leonardo Rick Aug 31 '20 at 23:37
  • May use without *_blank*. By default, a new tab opens. – Yury Matatov Sep 05 '20 at 10:10
79

just use the full url as href like this:

<a href="https://www.example.com/" target="_blank">page link</a>
Lux
  • 17,835
  • 5
  • 43
  • 73
zhimin
  • 2,740
  • 12
  • 22
36

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');
}
wentjun
  • 40,384
  • 10
  • 95
  • 107
35

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>
C.Ikongo
  • 1,706
  • 1
  • 16
  • 15
22
<a [routerLink]="" (click)="openSite(SiteUrl)">{{SiteUrl}}</a>

and in your Component.ts

openSite(siteUrl) {
   window.open("//" + siteUrl, '_blank');
}
Liam
  • 27,717
  • 28
  • 128
  • 190
17

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();
  }
Morlo Mbakop
  • 3,518
  • 20
  • 21
13

In the app-routing.modules.ts file:

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

In the component.html file:

target="_blank" [routerLink]="['/hero', '/sachin']"
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Femina
  • 141
  • 1
  • 5
5

Try this:

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

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

a_hardin
  • 4,991
  • 4
  • 32
  • 40
DINESH Adhikari
  • 1,242
  • 1
  • 12
  • 11
2

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

goToLink(url: string) {
    window.open('https://' + url, "_blank");
}
Tobias S.
  • 21,159
  • 4
  • 27
  • 45
1

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>

jdcreativemaker
  • 131
  • 1
  • 3