【问题标题】:Is there a built-in way to get the current URL without any query parameters?是否有一种内置方法可以在没有任何查询参数的情况下获取当前 URL?
【发布时间】:2014-06-23 17:00:51
【问题描述】:

如果我的网址是http://www.something.com/foo/bar/index.html?color=yellow&animal=rat,看起来好像:

  • $location.path() 将返回 foo/bar/index.html
  • $location.absUrl() 将返回 http://www.something.com/foo/bar/index.html?color=yellow&animal=rat
  • $location.url() 将返回 foo/bar/index.html?color=yellow&animal=rat

是否有任何函数会返回http://www.something.com/foo/bar/index.html

或者我是否必须自己使用协议、主机、端口等函数来构建它(或者我自己剥离查询参数)?

【问题讨论】:

  • 当我这样做时,我必须自己构建它。如果没有必要,我会很高兴。
  • $location.absUrl() 不是返回你想要的,或者你为什么不只是剥离?最后?
  • $location.absUrl() 末尾有查询参数 ("?color=yellow&animal=rat")。很容易剥离,但我只是想知道是否有内置功能,因为它们似乎具有几乎所有其他功能。
  • 您反对使用$window服务吗? $window.location.origin + $window.location.pathname 应该可以得到你想要的。
  • 看来只能自己构建了。

标签: angularjs url location-provider


【解决方案1】:

据我所知,您必须自己构建它。不是你问如何构建它,而是那些想知道的人:

var url = $location.absUrl().split('?')[0]

【讨论】:

【解决方案2】:

并不是说这消除了自己构建它的需要,只是另一种方式来做同样的事情。 如果你使用 window.location 对象,你可以说 window.location.origin+window.location.pathname

window.location 对象有

host:"localhost.abc.com:8080"
hostname:"localhost.abc.com"
href:"http://localhost.abc.com:8080/quickpick/repossessions/?displayStr=Repossessions&from=%2F&page=1"(whole url)

origin:"http://localhost.abc.com:8080"
pathname:"/quickpick/repossessions/"
port:"8080"
protocol:"http:"

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 2017-07-20
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2019-10-31
    相关资源
    最近更新 更多