如何将 ng-href 与绝对 url 一起使用? [英] How to use ng-href with absolute url?

查看:24
本文介绍了如何将 ng-href 与绝对 url 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是 angularjs 的新手,我有一个问题,我似乎无法找到解决方案,而且我没有时间去查看 angular 源.

I'm still new to angularjs, and I have a problem that I seem to not be able to find solution, and I don't have time to go look into angular source.

这是我的场景:

我有一些 json 数据,其中包含我想在屏幕上显示的 url 集合.我有一个带有 ng-repeat="link in links" 的元素,里面有

I have some json data with collection of urls that I want to show on screen. I have an element with ng-repeat="link in links" and inside I have

<a ng-href="{{link.url}}">{{link.title}}</a>

这可行,但所有链接都指向 mydomain/apppath/valueoflink.title我希望它们是绝对的,只有 valueoflink.title 没有任何前缀.

That works, but all links are pointing to mydomain/apppath/valueoflink.title I want them to be absolute, only valueoflink.title without any prefix.

如何告诉 angular 它是绝对的而不是相对的 url?

How to tell angular that it is absolute not relative url?

推荐答案

我通过在 json 中的数据前面加上 'http://' 来解决这个问题,使它们成为真正的绝对 url,angularjs 尊重这一点.

I solved it by prefixing data in json with 'http://' to make them trully absolute urls, and angularjs respects that.

然后我明白了 angular 实际上并没有做任何有价值的事情,它只是把它放在那里,看到它是我的错.

Then I understood that angular is actually not doing anything with value, it is just putting it there as it is, and it is my fault to see that.

当所有网址都像www.google.com"时,更新代码可以解决问题

updating code to this solves problem when all urls are like 'www.google.com'

<a ng-href="http://{{link.url}}">{{link.title}}</a>

普通的检查元素"发现了问题,我忽略了 ng-href 从 {{value}} 语法绑定的事实,所以这就是我第一次尝试放置 ng-href="'http://'+{{value}}" 失败并导致我过早地提出问题,但我不确定是否应该删除它,因为我可能不是唯一犯这种错误的人.

Plain old 'inspect element' uncovered issue, and I ignored the fact that ng-href binds from {{value}} syntax, so this is why my first attempt to put ng-href="'http://'+{{value}}" failed and led me to ask question prematurely, but I'm not sure if I should delete it as I may not be only one making such mistake.

这篇关于如何将 ng-href 与绝对 url 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆