无法使用href或ng-href转到外部网址 [英] Can't go to external urls using either href or ng-href
问题描述
我正在尝试使用一个动态URL,该URL使用AngularJS将用户带到外部网站.但是,单击该链接会将用户带到我页面上的一条路线.
I'm trying to have a dynamic URL that takes the user to an external website with AngularJS. However the link takes the user to a route on my page when clicked.
例如,如果我的网址是"mysite.com",而我的链接是"google.com".如果我使用href
或ng-href
单击此链接,它将带我到我的网站上不存在的路由:"mysite.com/google.com"
For example, if my url is "mysite.com", and my link is "google.com". If I click on this link, using either href
or ng-href
, it takes me to a non-existent route on my site: "mysite.com/google.com"
如何导航到外部URL?
How can I navigate to external URLs?
我的代码示例:
<a href="{{ linkItem.link }}" target="_blank">View file</a>
linkItem.link
是"google.com".对于任何网页,即使是具有硬编码的href属性的网页,这都无法正常工作.
Whereas linkItem.link
is "google.com". This doesn't work as expected for any webpage, even for the ones that have hard coded href attributes.
推荐答案
问题是由URL中没有前缀协议(即http://
或https://
)引起的,该协议使浏览器认为它是相对路径.有两种解决方法:
The problem is caused by not having a protocol (ie. http://
or https://
) prefixed in the URL which makes the browser think it's a relative path. There are two ways to fix it:
-
要求用户输入一个有效的URL,该URL开头应为
http://
或https://
.
创建一个过滤器或指令,以在缺少前缀时添加前缀.
Create a filter or directive that adds the prefix if it's missing.
这篇关于无法使用href或ng-href转到外部网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!