Ian Warburton Ian Warburton - 1 year ago 77
AngularJS Question

Getting values from query string in an url using AngularJS $location

Regarding $location.search, 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 '$location.search()' returns an object. How do I get the actual text?

Answer Source

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

Example (with larger query string): http://fiddle.jshell.net/TheSharpieOne/yHv2p/4/show/?test_user_bLzgB&somethingElse&also&something=Somethingelse

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)

As pointed out in the comments by @Naresh and @DavidTchepak, the $locationProvider also needs to be configured properly: https://code.angularjs.org/1.2.23/docs/guide/$location#-location-service-configuration

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download