在<a> (ignore base url) in

时间:2016-09-06 09:26:18

标签: javascript angularjs url

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.

2 个答案:

答案 0 :(得分:0)

是的,你应该使用类似的东西:

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

它会在每个没有网址的网址前添加http//:

答案 1 :(得分:0)

我决定使用:

fixUrl(item) {
    return '//' + item.replace(/^.*:\/\//, '');
}