Mandroid
Mandroid

Reputation: 7484

Opening a new tab for given url in angular 2

I am working on an angular component. This component displays list of items. Item contains basic information like date, text etc and an url. On clicking text, I need to open a new tab with the url.

I am using this:

window.open(url);

But I face issue. Suppose current url is www.myapplication.com, and url is www.google.com. With current implementation, new tab does open but instead of browser url as www.google.com, it is www.myapplication.com/www.google.com.

How to get it working ie opening new tab with www.google.com?

Upvotes: 1

Views: 521

Answers (2)

Md Rafee
Md Rafee

Reputation: 5530

Solution 1:

use http or https before your absolute link. otherwise it will think as a relative link. So, you have to use http or https:

window.open(this.url);

Solution 2

Just use the <a> tag and use target attribute and href to the link. and style <a> tag as you wish to.

<a target="_blank" [href]="link">Google</a>

Working solution of both: Stackblitz

Upvotes: 1

Daniel
Daniel

Reputation: 1042

You just need to put http:/, is the way to specify that a url is absoulte.

it's not a matter of angular, it's just javascript

window.open("http://www.google.com");

Upvotes: 1

Related Questions