21

I want to redirect user to another page after successful login using query string. If I copy & paste to browser's address bar http://example.com/#/login?ref=/path/to/redirect it works fine. But if I use

$location.path("/login?ref=/path/to/redirect");

url looks like

http://example.com/#/login%3Fref=/path/to/redirect

How can I decode %3F to '?' ? Thanks

3
  • Are you using ui-router? you should be able to do $state.go('state', {ref: 'params'} and pass in the query string. Commented Jun 12, 2014 at 15:43
  • 3
    See here: stackoverflow.com/questions/15197837/… Commented Jun 12, 2014 at 15:44
  • You can also use $routeParams and create a route that looks like this: /login/:ref and then ref would be a parameter in the $routeParams service. docs.angularjs.org/api/ngRoute/service/$routeParams Commented Jun 12, 2014 at 16:00

2 Answers 2

47

It should be

$location.path('/login').search('ref', '/path/to/redirect')
Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately this doesn't work if you have reloadOnSearch set to false on the route. Angular considers this a reload since it isn't set simultaneously. I should open and issue in angular.js for this but do you have an alternative to set path and query simultaneously (other than setting the location object directly which would cause a full reload)
What if you have some common component, that doesn't know anything about query string content? How can you pass it then?
0

I had the same problem.

I want to change path and search the same time.

It is possible by doing this:

$location.$$search ={foo:'bar', buz:'buz'}
$location.$$path = '/some/path'
$location.$$compose()

It is not documented and it could broke at any new angular version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.