Akshay Khetrapal Akshay Khetrapal - 1 year ago 67
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

to achieve this and running a simple JS
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 Source

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

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