How to get host base + from angular $ location

I have an angular application located in the 'someApp' directory. Url http://example-domain/someApp/#/ for some state url with the outline: http://example-domain/someApp/#/user/login . I have there an "angular path" to get this part http://example-domain/someApp/

 var redirectUri = $location.absUrl(); redirectUri = redirectUri.substr(0, redirectUri.indexOf('#')); 
+7
angularjs
source share
2 answers

The easiest way is to use

 window.location.origin + window.location.pathname 

which will return http://example-domain/someApp

This will provide the entire base url, even if you use virtual directories / paths. However, IE does not support the origin. As a result, you could combine the components of the URL to provide you with a base directory as follows:

 window.location.protocol + "//" + window.location.hostname + window.location.pathname 

which will return http://example-domain/someApp

If you use virtual directories, it will be difficult for you to use $location , since $location.path() returns AFTER the hash, and $location.host() returns only the domain, not the domain and directory, which means window.location.hostname + window.location.pathname .

+7
source share

You need to use:

  location.origin + location.pathname 

Say the URL is http://example.com/#/some/path?foo=bar&baz=xoxo

 $location.protocol(); // will give: http $location.host(); // will give: example.com location.host // will give: example.com:8080 $location.port(); // will give: 8080 $location.path(); // will give: /some/path $location.url(); // will give: /some/path?foo=bar&baz=xoxo 

Detailed description

+3
source share

All Articles