Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'd like to read the values of URL query parameters using AngularJS. I'm accessing the HTML with the following URL:

http://127.0.0.1:8080/test.html?target=bob

As expected, location.search is "?target=bob". For accessing the value of the target, I've found various examples listed on the web, but none of them works in AngularJS 1.0.0rc10. In particular, the following are all undefined:

  • $location.search.target

  • $location.search['target']

  • $location.search()['target']

Does anyone know what will work? (I'm using $location as a parameter to my controller)

1 Answer

0 votes
by (106k points)
edited by

The most concise way to read query parameter in AngularJS is if you can inject $routeParams (requires ngRoute) into your controller. 

Are you interested in learning Angularjs from the basics! Here's the right video for you on Angularjs provided by Intellipaat:

Here's an example from the docs:

// Given: 

// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby 

// Route: /Chapter/:chapterId/Section/:sectionId 

// Then $routeParams ==> {chapterId:1, sectionId:2, search:'moby'}

Browse Categories

...