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

Jer*_*Jer 26 url location-provider angularjs

如果我的网址是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等函数构建自己(或者自己删除查询参数)?

Jam*_*amy 38

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

var url = $location.absUrl().split('?')[0]
Run Code Online (Sandbox Code Playgroud)

  • 也尝试$ location.$$路径和$ location.$$ url (2认同)
  • @BadshahTracker试试这个:`var url = $ location.absUrl();` (2认同)

cha*_*ndu 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:"
Run Code Online (Sandbox Code Playgroud)