atomty
atomty

Reputation: 187

Interpolation in angularjs

 <a href="{{link}}" target="_blank">{{link}}</a>

"Link" is the url gotten from the database. On click of link, I want the browser to open the link. The problem is that: lets assume the name of my website is "stackoverflow.com". If the link is "facebook.com", on click of this link, it will open "stackoverflow.com/facebook.com" which is not what I want. But if the link is "https://facebook.com", it will open "facebook.com", exactly what I want. The question is that: how can I make the browser open "facebook.com" whether the link is "facebook.com" or "https://facebook.com"

Upvotes: 0

Views: 43

Answers (2)

rahim.nagori
rahim.nagori

Reputation: 666

You may use something like this :

<a ng-href={{link}} target="_blank">{{link}}<a>

And in your AngularJs controller you may apply a validation check like this :

if( link doesn't have http://){
    link = 'http://' + link;
}

This way your link will always starts with http://

Upvotes: 0

felixmosh
felixmosh

Reputation: 35613

This is the default behaviour of browsers, when you are not specifying an absolute path (the one that is without http:// prefix) it considered as a relative path, therefore it navigates relative to the current domain.

You can attach the http:// prefix on the client side.

Side note, it is better to use the ng-href directive that prevents from none interpolated urls to active.

 <a ng-href="http://{{link}}" target="_blank">{{link}}</a>

Upvotes: 1

Related Questions