document.location.pathname
和$location.path()
之间存在不匹配。
我有一个像http://myhost.com/services/coolstuff
一样的角度SPA。
当您导航到该位置时,我会使用一些路由器规则来导航您:http://myhost.com/services/coolstuff#/users/1
。
此时,您可以致电$location.path()
,它会给您/users/1
。 document.location.pathname
为您提供/services/coolstuff
。
是否有机制从$location
检索实际的网址路径?我不想要Angular散列路径(/users/1
),而是实际的URL路径(/services/coolstuff
)。我怎么能得到它?
答案 0 :(得分:0)
这里的聚会有点晚了,但是parsing section of the URL specification对此非常具体(阅读起来很繁琐……):
以上规范的要旨是,未编码的#
字符表示URL fragment
的开始,而path
并非$location.path()
的一部分。
这实质上意味着/services/coolstuff
在这里的行为不正确,应该像document.location.pathname
一样正确地返回ref.on()
。
通常,如果本机JS支持某些功能,则我总是选择这样做,因为您可以确保(如从示例中看到的)浏览器实现符合指定的标准,而第三种通常不是这种情况政党图书馆。