28

如果我的 URL 是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吗?

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

4

2 回答 2

43

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

var url = $location.absUrl().split('?')[0]
于 2015-11-17T16:54:11.250 回答
9

并不是说这消除了自己构建它的需要,只是另一种方式来做同样的事情。如果你使用 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:"
于 2017-11-20T22:29:12.413 回答