alecswan alecswan - 5 months ago 20
AngularJS Question

AngularJS: How to clear query parameters in the URL?

My AngularJS application needs to have access to the user's LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback redirect_uri parameter which will tell LinkedIn to redirect the user back to my webapp and include a "code" query param in the URL. It's a traditional Oauth 2.0 flow.

Everything works great except that LinkedIn redirects the user back to the following URL:

http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites


I would like to remove
?code=XXX&state=YYY
from the URL in order to make it clean. The user does not need to see the query parameters I received from LinkedIn redirect.

I tried
$location.absUrl($location.path() + $location.hash()).replace()
, but it keep the query params in the URL.

I am also unable to extract the query parameters, e.g. "code", using
($location.search()).code
.
It seems like having ? before # in the URL above is tricking Angular.

Answer

I ended up getting the answer from AngularJS forum. See this thread for details


The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use

$location.url($location.path());