9952 explained code solutions for 126 technologies

angularjsHow do I get query parameters using AngularJS?

Getting query parameters using AngularJS is quite easy. To do so, you can use the $location service. $location provides several methods to get query parameters. You can use $location.search() to get all the query parameters as a key-value object.

For example, if the URL is http://example.com/?name=John&age=25, then you can get the query parameters using:

var queryParams = $location.search();
// Output: { name: 'John', age: '25' }

Here are the parts of the code and what they do:

  • var queryParams =: This declares a variable to store the query parameters.
  • $location.search(): This is a method of the $location service which returns an object containing the query parameters.
  • console.log(queryParams): This prints the query parameters to the console.

Here are some helpful links for further reading:

Edit this code on GitHub