0

In IIS, my web app runs in an application under the website. So instead of being accessible at www.example.com, it is actually at www.example.com/somepath/.

If I use window.location.pathname, I can get my hands on the /somepath/ portion, but with Angular's $location.path() I get nothing. From the official documentation, it seems like $location.path() should only get whatever comes after the hash, e.g. www.example.com/somepath/#/nextbit will give me /nextbit.

So how do I get my hands on the actual path where the website is hosted?

Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106
  • Possible duplicate of [Get current URL in web browser](http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser) – Ryan Oct 03 '16 at 16:11
  • `absUrl` is what you need. – Casey Oct 03 '16 at 16:25
  • @Ryan No, this is different. I'm not looking for the full URL (then absUrl would be the full answer). This is specifically about getting the path of the site, not the page. In my example, the aim is to get `/somepath/` whereas location.href will give the full path. – Cobus Kruger Oct 04 '16 at 08:19

2 Answers2

2

As you want to do it using Angular $location service, try this:

var baseUrl = $location.absUrl().replace('#/' + $location.url(), '');

absUrl() will get the whole URL, while url() will get the part of the url after #/

user449689
  • 3,142
  • 4
  • 19
  • 37
0

Since the information coming before the hash is not considered superbly relevant to Angular's internal workings, I don't think bypassing Angular's internal method ($location) would be a bad practice in this case.

I would go with your original assumption, since it will still work:

location.pathname

Unless you're worried about the bindings inside of Angular having fired prior to getting that path (which shouldn't have an affect on a static, IIS application path), this should be fine.

frankrue
  • 74
  • 5