0

Is there an easier/shorte/AngularJS way to make the conversion of parameters from URL to a JavaScript object shorter than

JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}')  

(the answer above pulled from SO https://stackoverflow.com/questions/8648892/convert-url-parameters-to-a-javascript-object

What needs to be done is to convert a long string with many parameters such as

field1=value1&field2=value2...

into an object

Object {field1: "value1",field2: "value2",...}

Community
  • 1
  • 1
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
  • 1
    both the angular router and the 3rd party ui-router automatically convert URL parameters to individual objects, and the `$location` service also can expose URL parameters. can you show the code that you are currently using, including any router, if applicable? – Claies Jun 27 '16 at 12:16
  • Sorry, unfortunately I can't share more code. It's not in the current URL. It's a variable that is a string. – Haradzieniec Jun 27 '16 at 12:32
  • why do you have a variable that is an HTML parameter list? That isn't a normal angular practice... – Claies Jun 27 '16 at 15:01
  • also, since the code you posted *technically works*, and you aren't willing/able to post any more code illustrating why this isn't suitable, it's very difficult to make suggestions about better alternatives, and it's questionable if this is even useful to others in the future. – Claies Jun 27 '16 at 15:03

0 Answers0