是否有内置方法来获取当前URL而没有任何查询参数?

时间:2014-05-06 22:26:54

标签: angularjs url location-provider

如果我的网址为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

或者我是否必须使用protcol,host,port等功能构建自己(或者自己删除查询参数)?

2 个答案:

答案 0 :(得分:37)

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

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

答案 1 :(得分:8)

并不是说这不需要自己构建它,只是另一种方法来做同样的事情。 如果你使用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:"