ngroute - What's the most concise way to read query parameters in AngularJS? -


i'd read values of url query parameters using angularjs. i'm accessing html following url:

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

as expected, location.search "?target=bob". accessing value of target, i've found various examples listed on web, none of them work in angularjs 1.0.0rc10. in particular, following undefined:

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

anyone know work? (i'm using $location parameter controller)


update:

i've posted solution below, i'm not entirely satisfied it. documentation @ developer guide: angular services: using $location states following $location:

when should use $location?

any time application needs react change in current url or if want change current url in browser.

for scenario, page opened external webpage query parameter, i'm not "reacting change in current url" per se. maybe $location isn't right tool job (for ugly details, see answer below). i've therefore changed title of question "how read query parameters in angularjs using $location?" "what's concise way read query parameters in angularjs?". use javascript , regular expression parse location.search, going low-level basic offends programmer sensibilities.

so: there better way use $location in answer, or there concise alternate?

you can inject $routeparams (requires ngroute) controller. here's example docs:

// given: // url: http://server.com/index.html#/chapter/1/section/2?search=moby // route: /chapter/:chapterid/section/:sectionid // // $routeparams ==> {chapterid:1, sectionid:2, search:'moby'} 

edit: can , set query parameters $location service (available in ng), particularly search method: $location.search().

$routeparams less useful after controller's initial load; $location.search() can called anytime.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -