javascript – Getting values from query string in an url using AngularJS $location-ThrowExceptions

Exception or error:

Regarding $, the docs say,

Return search part (as object) of current url when called without any parameter.

In my url, my query string has a param ‘?test_user_bLzgB’ without a value. Also ‘$’ returns an object. How do I get the actual text?

How to solve:

Not sure if it has changed since the accepted answer was accepted, but it is possible.

$ will return an object of key-value pairs, the same pairs as the query string. A key that has no value is just stored in the object as true. In this case, the object would be:

{"test_user_bLzgB": true}

You could access this value directly with $

Example (with larger query string):

Note: Due to hashes (as it will go to, which would create a new fiddle), this fiddle will not work in browsers that do not support js history (will not work in IE <10)

As pointed out in the comments by @Naresh and @DavidTchepak, the $locationProvider also needs to be configured properly:$location#-location-service-configuration


If you just need to look at the query string as text, you can use: $


$ returns an object, consisting of the keys as variables and the values as its value.
So: if you write your query string like this:


You could easily get the text like so:


If you wish not to use a key, value like ?foo=bar, I suggest using a hash #test_user_bLzgB ,

and calling


would return ‘test_user_bLzgB’ which is the data you wish to retrieve.

Additional info:

If you used the query string method and you are getting an empty object with $,
it is probably because Angular is using the hashbang strategy instead of the html5 one…
To get it working, add this config to your module

yourModule.config(['$locationProvider', function($locationProvider){


First make the URL format correct for getting the query string use #?q=string that works for me


Inject $location service into the controller

app.controller('MyController', [ '$location', function($location) { 

    var searchObject = $;

    // $; reutrn object 
    // searchObject = { foo = 'abcd' };

    alert( );

} ]);

So the out put should be abcd


you can use this as well

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(;
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));

var queryValue = getParameterByName('test_user_bLzgB');


If your $ is not working, then make sure you have the following:

1) html5Mode(true) is configured in app’s module config

appModule.config(['$locationProvider', function($locationProvider) {

2) <base href="/"> is present in your HTML

  <base href="/">


  1. base href=”/”
  2. html5Mode


Angular does not support this kind of query string.

The query part of the URL is supposed to be a &-separated sequence of key-value pairs, thus perfectly interpretable as an object.

There is no API at all to manage query strings that do not represent sets of key-value pairs.


In my NodeJS example, I have an url “localhost:8080/Lists/list1.html?x1=y” that I want to traverse and acquire values.

In order to work with $ to get x1=y, I have done a few things

  1. script source to angular-route.js
  2. Inject ‘ngRoute’ into your app module’s dependencies
  3. Config your locationProvider
  4. Add the base tag for $location (if you don’t, your search().x1 would return nothing or undefined. Or if the base tag has the wrong info, your browser would not be able to find your files inside script src that your .html needs. Always open page’s view source to test your file locations!)
  5. invoke the location service (search())

my list1.js has

    var app = angular.module('NGApp', ['ngRoute']);  //dependencies : ngRoute
    app.config(function ($locationProvider) { //config your locationProvider

    app.controller('NGCtrl', function ($scope, datasvc, $location) {// inject your location service
        //var val = window.location.href.toString().split('=')[1];
        var val = $;    alert(val);
        $scope.xout = function () {
           .then(function (data) {
              $scope.x1 = val;
              $scope.allMyStuffs = data.all;

and my list1.html has

    <base href=".">
<body ng-controller="NGCtrl">
<div>A<input ng-model="x1"/><br/><textarea ng-model="allMyStuffs"/></div>
<script src="../js/jquery-2.1.4.min.js"></script>
<script src="../js/jquery-ui.min.js"></script>
<script src=""></script>
<script src=""></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/ui-bootstrap-tpls-0.14.3.min.js"></script>
<script src="list1.js"></script>



My fix is more simple, create a factory, and implement as one variable. For example

angular.module('myApp', [])

// This a searchCustom factory. Copy the factory and implement in the controller
.factory("searchCustom", function($http,$log){    
    return {
        valuesParams : function(params){
            paramsResult = [];
            params = params.replace('(', '').replace(')','').split("&");
            for(x in params){
                paramsKeyTmp = params[x].split("=");
                // Si el parametro esta disponible anexamos al vector paramResult
                if (paramsKeyTmp[1] !== '' && paramsKeyTmp[1] !== ' ' && 
                    paramsKeyTmp[1] !== null){          
            return paramsResult;

.controller("SearchController", function($scope, $http,$routeParams,$log,searchCustom){
    $ctrl = this;
    var valueParams = searchCustom.valuesParams($routeParams.value);
    valueParams = valueParams.join('&');

        method : "GET",
        url: webservice+"q?"+valueParams
    }).then( function successCallback(response){
        data =;
        $scope.cantEncontrados = data.length; 
        $scope.dataSearch = data;
    } , function errorCallback(response){
<body ng-app="myApp">
<div ng-controller="SearchController">
<form action="#" >
                            <input ng-model="param1" 
                                   placeholder="param1" />
                            <input  ng-model="param2" 
<!-- Implement in the html code 
(param1={{param1}}&param2={{param2}}) -> this is a one variable, the factory searchCustom split and restructure in the array params
                        <a href="#seach/(param1={{param1}}&param2={{param2}})">
                            <buttom ng-click="searchData()" >Busqueda</buttom>

Leave a Reply

Your email address will not be published. Required fields are marked *