angular的$ location中的document.location.pathname相当于什么?

时间:2016-06-22 19:03:30

标签: angularjs url

document.location.pathname$location.path()之间存在不匹配。

我有一个像http://myhost.com/services/coolstuff一样的角度SPA。

当您导航到该位置时,我会使用一些路由器规则来导航您:http://myhost.com/services/coolstuff#/users/1

此时,您可以致电$location.path(),它会给您/users/1document.location.pathname为您提供/services/coolstuff

是否有机制从$location检索实际的网址路径?我不想要Angular散列路径(/users/1),而是实际的URL路径(/services/coolstuff)。我怎么能得到它?

1 个答案:

答案 0 :(得分:0)

这里的聚会有点晚了,但是parsing section of the URL specification对此非常具体(阅读起来很繁琐……):

以上规范的要旨是,未编码的#字符表示URL fragment的开始,而path并非$location.path()的一部分。

这实质上意味着/services/coolstuff在这里的行为不正确,应该像document.location.pathname一样正确地返回ref.on()

通常,如果本机JS支持某些功能,则我总是选择这样做,因为您可以确保(如从示例中看到的)浏览器实现符合指定的标准,而第三种通常不是这种情况政党图书馆。