Not sure if it has changed since the accepted answer was accepted, but it is possible.
$location.search() 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 $location.search().test_user_bLzgB
Note: Due to hashes (as it will go to http://fiddle.jshell.net/#/url, 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)
$location.search() returns an object, consisting of the keys as variables and the values as its value.
So: if you write your query string like this:
?user=test_user_bLzgB
You could easily get the text like so:
$location.search().user
If you wish not to use a key, value like ?foo=bar, I suggest using a hash #test_user_bLzgB ,
and calling
$location.hash()
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 $location.search(),
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
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 $location.search() to get x1=y, I have done a few things
script source to angular-route.js
Inject 'ngRoute' into your app module's dependencies
Config your locationProvider
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!)
invoke the location service (search())
my list1.js has
var app = angular.module('NGApp', ['ngRoute']); //dependencies : ngRoute
app.config(function ($locationProvider) { //config your locationProvider
$locationProvider.html5Mode(true).hashPrefix('');
});
app.controller('NGCtrl', function ($scope, datasvc, $location) {// inject your location service
//var val = window.location.href.toString().split('=')[1];
var val = $location.search().x1; alert(val);
$scope.xout = function () {
datasvc.out(val)
.then(function (data) {
$scope.x1 = val;
$scope.allMyStuffs = data.all;
});
};
$scope.xout();
});
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){
paramsResult.push(params[x]);
}
}
return paramsResult;
}
}
})
.controller("SearchController", function($scope, $http,$routeParams,$log,searchCustom){
$ctrl = this;
var valueParams = searchCustom.valuesParams($routeParams.value);
valueParams = valueParams.join('&');
$http({
method : "GET",
url: webservice+"q?"+valueParams
}).then( function successCallback(response){
data = response.data;
$scope.cantEncontrados = data.length;
$scope.dataSearch = data;
} , function errorCallback(response){
console.log(response.statusText);
})
})
<html>
<head>
</head>
<body ng-app="myApp">
<div ng-controller="SearchController">
<form action="#" >
<input ng-model="param1"
placeholder="param1" />
<input ng-model="param2"
placeholder="param2"/>
<!-- Implement in the html code
(param1=\{\{param1}}¶m2=\{\{param2}}) -> this is a one variable, the factory searchCustom split and restructure in the array params
-->
<a href="#seach/(param1=\{\{param1}}¶m2=\{\{param2}})">
<buttom ng-click="searchData()" >Busqueda</buttom>
</a>
</form>
</div>
</body>
Very late answer :( but for someone who is in need,
this works Angular js works too :) URLSearchParams Let's have a look at how we can use this new API to get values from the location!