Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $location.path('/') does not clear search() parameters

Tags:

angularjs

I am using

$location.path('/login');

to redirect back to a login page if a user is not logged in, or in general use this to redirect anywhere. But If I already have an url that looks like this

/register/final-step?token=mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf

and redirect to

$location.path('/');

then the search parameters do not get cleared, so I get an url like this

/?token=mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf

Even if I use UI-Router's

$state.go('home');

the

?token=mF6xY2cQvB9Vccb0J1l5uTu4H10lWkkf

part is still there.

I don't understand. Do I really have to clear parameters manually this every time I need to go to another state or url? This does not seem very legit to me. What do I do wrong? I don't want to do window.location.href because that makes a full page reload which is not something I want to do.

What should I do to clear the parameters? I must be doing something wrong.

like image 672
Alex Avatar asked May 27 '15 00:05

Alex


1 Answers

The part of the url after the ? is called the search (in angular -- in other things it may be referred to as the query). You were dealing with only the path, which is the part before the ?.

You can use it like this (and yes, it's used for way more than just searches...but that's its name in this context).

// Get the current value
var curr_search = $location.search();

// Clear the current search
$location.search({});

// Set the current search values
$location.search({key: "value"});
like image 178
Nathan Stocks Avatar answered Sep 29 '22 05:09

Nathan Stocks