1

In Angular app I have <base href="/foo/bar/"> and $locationProvider.html5Mode(true);

I want to create some list:

<div ng-repeat="item in vm.items">
  <a ng-href="{{item}}" target="_blank">{{item}}</a>
  <a ng-href="{{item}}" download>{{item}}</a>
</div>

My items can look like this: ['example1.com', 'www.example2.com', 'http://www.example3.com', 'https://www.example4.com'];

For example3 and example4 it works ok. What is the best way to fix it for:

  • example1.com - now it goes to http://localhost/foo/bar/example1.com
  • www.example2.com - now it goes tohttp://localhost/foo/bar/www.example2.com

How can I force it to use absolute url? It should work with http, https and with download attribute.

Nikhilesh K V
  • 1,480
  • 13
  • 20
zucker
  • 1,066
  • 12
  • 26
  • Well i'm not sure in understand, use `http://www.example.com` ? – 0x01 Sep 06 '16 at 09:27
  • `example1.com` isn't an absolute URL. If you want to force it then you have to guess what scheme you want to use and change it. – Quentin Sep 06 '16 at 09:28
  • @MEGADEVOPS I import this items from external source. I can modify it with some regexp, but maybe there is easier way – zucker Sep 06 '16 at 09:29
  • make a filter see here http://stackoverflow.com/questions/21623967/how-to-use-ng-href-with-absolute-url – Cyril Cherian Sep 06 '16 at 09:44

2 Answers2

0

Yeah then you should use something like that :

if (strpos($url, 'http') == false) {
  $url = 'http//:' .$url;
}

It will add http//: in front of every URLs that doesn't have it.

0x01
  • 885
  • 7
  • 17
0

I've decided to use:

fixUrl(item) {
    return '//' + item.replace(/^.*:\/\//, '');
}
zucker
  • 1,066
  • 12
  • 26