无法使用href或ng

编程入门 行业动态 更新时间:2024-10-28 16:27:19
本文介绍了无法使用href或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",而我的链接是"google".如果我使用href或ng-href单击此链接,它将带我到我的网站上不存在的路由:"mysite/google"

For example, if my url is "mysite", and my link is "google". If I click on this link, using either href or ng-href, it takes me to a non-existent route on my site: "mysite/google"

如何导航到外部URL?

How can I navigate to external URLs?

我的代码示例:

<a href="{{ linkItem.link }}" target="_blank">View file</a>

linkItem.link是"google".对于任何网页,即使是具有硬编码的href属性的网页,这都无法正常工作.

Whereas linkItem.link is "google". This doesn't work as expected for any webpage, even for the ones that have hard coded href attributes.

推荐答案

问题是由URL中没有前缀协议(即或)引起的,该协议使浏览器认为它是相对路径.有两种解决方法:

The problem is caused by not having a protocol (ie. or ) prefixed in the URL which makes the browser think it's a relative path. There are two ways to fix it:

  • 要求用户输入一个有效的URL,该URL开头应为或.

    创建一个过滤器或指令,以在缺少前缀时添加前缀.

    Create a filter or directive that adds the prefix if it's missing.

  • 更多推荐

    无法使用href或ng

    本文发布于:2023-10-12 17:46:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1485423.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:href   ng

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!