0

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!

0

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can use $location for dealing with query params.

Eg: Your URL be like

http://blah.com?q1=aa&q2=bb

then

$location.search()

will return you a neat object `

{ q1 : "aa", q2 : "bb" }`

Be sure you inject $location in your controller!!

Read more in the official docs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.