angular2 routing - Building url with query parameters for Angular 2 Location -


i have implemented searchcomponent user can modify parameters search terms , facets filtering down results. wish update url selected parameters user can bookmark or email link.

my question updating url include parameters. know should using location service , go() method cannot seem figure out method(s) use take collection of parameters (either array or javascript object) , turn appropriately formatted query string based on current locationstrategy. supposed loop through collection , concatenate values directly build out single string?

code snippets

after posting have continued playing around api. understand it, using hashlocationstrategy , given following url

#/search 

the output of this.location.path(false) is

/search 

(as side note, output of this.location.path(true) also /search)

when use routing change url

#/search;searchterms=ftp 

the output of this.location.path(false) logically (kind of) becomes

/search;searchterms=ftp 

i have several points of concern

  1. as stated before, building out url seems require me concatenate name value pairs instead of providing helper method handles you.
  2. right now, given component ignorant of route used instantiate said component. actual route depends on put in router config. now, in order update route cannot "add particular current route", need extract prefix current url use in updated url.
  3. related item #2, need take value returned path() , split or index ; character can re-use non-parameters portion?

ultimately, in mind, component updating path based on changes within component seems very common use-case yet api seems make inconvenient , ugly manual process. hope missing something...

i mean, if wanted take approach, can use encodeuricomponent(btoa(json.stringify(object))) generate query string , json.parse(atob(decodeuricomponent(string))) object...

but i'd recommend looping, said.


Comments