pratap
pratap

Reputation: 117

Extract URL query parameters with AngularUI

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

Upvotes: 1

Views: 964

Answers (2)

Radim Köhler
Radim Köhler

Reputation: 123861

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

Upvotes: 1

mweathers
mweathers

Reputation: 951

inject $routeParams and use:

$routeParams.categoryID

to get the value.

Upvotes: 0

Related Questions