AngularJS: How to clear query parameters in the URL?

22,936 views
Skip to first unread message

alec...@gmail.com

unread,
Jul 1, 2013, 3:25:19 PM7/1/13
to ang...@googlegroups.com
Sorry for the crosspost with Stackoverflow, but I didn't get any answers there in a couple of days. I will reference the answer and close the other thread.

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:


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.

Any help will be greatly appreciated.

Rodrigo Kochenburger

unread,
Jul 1, 2013, 4:18:08 PM7/1/13
to ang...@googlegroups.com
If you're interacting w/ LinkedIn OAuth authorization server directly from Javascript, then you want to use the implicit grant flow: http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-1.3.2

If you're handling on the server side than you can just redirect to the client url, which does not include the code/state, after handling it.

Cheers

alec...@gmail.com

unread,
Jul 3, 2013, 7:32:33 PM7/3/13
to ang...@googlegroups.com
Thanks, Rodrigo. I ended up implementing your server-side option where LinkedIn redirects to my GET REST method, which stores authorizationCode and redirects the user to a page without query parameters.

However, I am still curious if it is possible to clear URL query parameters with Angular?

Thanks,

Alec

Josh David Miller

unread,
Jul 3, 2013, 7:58:03 PM7/3/13
to angular

Hello!

If you're using html5 mode, then there is no difference between standard query parameters and AngularJS ones. So your redirected URL would have looked like this:

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

And then you could clear the search/query parameters normally:

$location.search( 'code', null );
$location.search( 'state', null );

But I don't know of any built-in way to clear query parameters without html5 mode.

Josh
(Sent from my mobile)

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rodrigo Kochenburger

unread,
Jul 3, 2013, 8:02:01 PM7/3/13
to ang...@googlegroups.com
AFAIK there is no way to alter the URL through js without causing a redirect, besides using HTML5's history api.

alec...@gmail.com

unread,
Jul 3, 2013, 10:04:20 PM7/3/13
to ang...@googlegroups.com
Thank you all for responding.

Michael Wulf

unread,
Jul 4, 2013, 11:03:29 AM7/4/13
to ang...@googlegroups.com
I haven't enabled HTML5 in my Angular, but I was able to clear a search param by using the following setup:

// in the routing calls, add reloadOnSearch=false
$routeProvider.when('/about', {
            templateUrl: 'partials/about.html',
            controller: 'Controller'
            reloadOnSearch: false
         });

Now to remove the search, I use the same approach others have noted:

$location.replace();
$location.search('term', null);

Hope that's helpful to someone else; it saved my bacon.


On Monday, July 1, 2013 12:25:19 PM UTC-7, alec...@gmail.com wrote:

fess

unread,
Jul 4, 2013, 2:01:50 PM7/4/13
to ang...@googlegroups.com
I think this will remove a search param stored in the hash:

http://domain.com/base.index.html#!?search=blah

but it can't be used to remove one stored in the url

http://domain.com/base.index.html?search=blah

changing anything but the hash without html5 history will result in a browser reload.

(correct me if I'm wrong.)

--fess

Адельмурзин Ильнур

unread,
Oct 21, 2013, 12:46:37 AM10/21/13
to ang...@googlegroups.com
Try this code
$location.url('/');
$rootScope.$apply();

вторник, 2 июля 2013 г., 1:25:19 UTC+6 пользователь alec...@gmail.com написал:
Reply all
Reply to author
Forward
0 new messages