Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What's the best way to parse a query string in Angular without using html5mode? (Not using html5mode because we need to support older browsers)

I get the same undefined results whether or not I use a hash:

http://localhost/test?param1=abc&param2=def
http://localhost/test#/param1=abc&param2=def

$routeParams and $location.search() both return undefined:

var app = angular.module('plunker', ["ngRoute"]);

app.controller('MainCtrl', ["$scope", "$routeParams", "$location",
  function($scope, $routeParams, $location) {

  console.log($routeParams, $routeParams.abc); //undefined, undefined
  console.log($location.search(), $location.search().abc); //undefined, undefined

}]);

I can parse the window.location.search myself, but I'm hoping there is a better way to do it in Angular.

Plnkr: http://plnkr.co/edit/alBGFAkfqncVyK7iv8Ia?p=preview

I've read this post and haven't found a solution. I must be missing something. Thanks for the help.

share|improve this question
    
So the problem with your plunker will probably be because of it being inside of an iframe. Is the same issue happening locally? –  Michael Tempest Dec 19 '13 at 22:08
    
Yes, same results locally. Thanks for the help. –  Ender2050 Dec 19 '13 at 22:17

1 Answer 1

up vote 19 down vote accepted

the query params should go after the hash:

http://localhost/test#/?param1=abc&param2=def

this should allow $location.search() to return an object like:

{
  param1: 'abc',
  param2: 'def'
}
share|improve this answer
2  
Agh - I didn't realize you needed both the # and the ?. That makes total sense - thank you! –  Ender2050 Dec 19 '13 at 22:40
    
Or simply you can use $routeParams. –  Kariem Muhammed Jan 6 at 11:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.