angularjs - null is getting returned when $location.search is used -
i using $location.search params url gives null. url http://localhost/site/?user=12345.
app.controller('urlcontroller', ['$scope', '$log','$location', function($scope,$log,$location) { var testsearch = $location.search(); console.log(testsearch); }]) i want value 12345 user parsing url. how can achieved.
how can parse params url. checked documentation https://docs.angularjs.org/api/ng/service/$location
you need use $window
so:
var qstring = $window.location.search.substring(1);
the substring(1) removes ?
you can use function, one, parse object
string.prototype.parsequerystring = function () { var query = {}; var = this.split('&'); (var in a) { var b = a[i].split('='); query[decodeuricomponent(b[0])] = decodeuricomponent(b[1]); } return query; } and call with:
var qparts = qstring.parsequerystring();
issues $location
there known issues $location depending on html5 mode etc. $window doesn't have issue. e.g https://github.com/angular/angular.js/issues/7239
Comments
Post a Comment