How to set the $location to $location.host

44 Views Asked by At

so $location.path is www.abc.com/portal and host is www.abc.com I am trying to set the location as www.abc.com

$location.path($location.host);
3

There are 3 best solutions below

0
chinmayan On

I think you'll have to use $scope.$apply() after setting $location.path() inorder to make that work.

0
Luca Vimercati On

Because you need to redirect to main route. So try $location.path("/"); it add string after your $location.host();

For example:

// suppose $location.host = 'www.abc.com'
$location.path('/example')
// redirect to 'www.abc.com/#/example'
$location.path('/')
// redirect to your "otherwise" [route][1] if '/' isn't define 'www.abc.com/#/'

See documentation here:

path([path]);

This method is getter / setter.

Return path of current URL when called without any parameter.

Change path when called with parameter and return $location.

Note: Path should always begin with forward slash (/), this method will add the forward slash if it is missing.

0
Ratan Kumar On

I was trying to change the entire url means from www.abc.com/portal to www.abc.com

It works with

$location.href = location.origin;

Because location.host only contains

www.abc.com

and that was going to concatinate with

www.abc.com/portal/www.abc.com

using location.origin I was getting

https:// www.abc.com

so It is redirecting now.