Akshay Khetrapal Akshay Khetrapal - 3 months ago 9
AngularJS Question

How to get query params in an Angular app without the need of state or route

I need query params in the url for my Single-Page Angular app. I simply need to access this data.

Currently, I am using

$location.absUrl()
to achieve this and running a simple JS
split('?')
on it which gives me the query.

Is there any better way to achieve this without getting into the complexities of router or state.

P.S. Picked up Angular today. Go easy!

Answer

You can use $location.search() method. It returns an object of all url parameters and their values.

// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'} 

This example is taken from the official Angular documentation about $location service. https://docs.angularjs.org/api/ng/service/$location