1

I am using AngularUI library and want to extract the query parameters with from a URL (sample URL: #\books\:categoryID?publisher=xyz (#\books\value?publisher=xyz)).

The $stateParams extracts the data as {categoryId:value?publisher=xyz} but I need to get it as {categoryId:"value", publisher:"xyz"}.

Thanks in advance

2 Answers 2

1

Angular ui-router has direct support for passing parameters as a query string params. There is a doc

There is a link to an example, using this state definition:

  $stateProvider
    .state('books', {
      url : '/books/:categoryID?publisher',
      template: '<div> This is a state def: '+
      '<pre>{{toNiceJson(state)}}</pre>' +
      ' These are params sent to this state:' +
      '<pre>{{toNiceJson(params)}}</pre>'+ 
      ' </div>',
      controller: 'urlParamsCtrl', 
    }) 

And these could be links to get to that state

<a href="#/books/value?publisher=xyz">...
<a href="#/books/other?publisher=unknown%20publisher">...
<a ui-sref="books({categoryID:'value', publisher:'xyz'})">...
<a ui-sref="books({categoryID:'other', publisher:'unknown publisher'})">...

See more here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!!! that worked. I had an old code snippet that was using $location for setting a new url and ui-router was not able to get the query parameters. After seeing your example I realized I should be using the state.go('myState', {state parameters}) and got it working. Thanks
Exactly! I would say that ui-router is most likely the best choice you can do ;)
0

inject $routeParams and use:

$routeParams.categoryID

to get the value.

1 Comment

that returns {categoryId:value?publisher=xyz} ngRoute doesn't have enough support for query params and maintaining nested views that's when I switched to angular-ui.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.