How to open link in new tab in angular 5

How to open link in new tab in angular 5



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?






This works for me <a href="//example.com" target="_blank">page link</a>

– M Mansour
Sep 9 '18 at 10:01


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




3 Answers
3



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


window.open()



In your component.html file-


component.html


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



In your component.ts file-


component.ts


goToLink(url: string)
window.open(url, "_blank");



just use the full url as href like this:


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



Perhaps try something like this


href="https://www.example.com"






Isn't this a regular link? How does this open in a new tab?

– ANeves
Jan 21 at 18:59






yes, it requires a target

– Mathias
Jan 22 at 19:03



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.