3

I use Angular 1.5 and ui.router module and want to pas int array to app throw url. www.example.com/#/filter?[1,2,5] or www.example.com/#/filter/[1,2,5] or something else.

.state('cats', {
             url: '/filter?catsIds', // or /filter/{catsIds}
             templateUrl:'cats.html',
             controller: 'catsCtrl'
         })

goal:

app.controller('catsCtrl',function($stateParams){
    console.log(Array.isArray($stateParams.catsIds)) // goal true
    ...
1
  • noticed your example url is using incorrect array parameter, try using commas e.g. www.example.com/#/filter?catsIds=1,2,5 Commented Mar 31, 2016 at 18:45

2 Answers 2

7

You can't pass object through an ui-router parameter, I'd say while passing value make it , separated & then pass it.

So while calling state do something like below

$state.go('cats', { catsIds: [1,2,5].join(',') })

will form below URL

www.example.com/#/filter/1,2,5

While reading it from $stateParams do split that string by ,

app.controller('catsCtrl',function($stateParams){
    console.log($stateParams.catsIds.split(','))
Sign up to request clarification or add additional context in comments.

Comments

0

As stated in the ui-router doc, one can actually pass an url parameter several time and it will be mapped as an array of values if the parameter is declared with 'array:true' in the routing configuration :

in your url :

www.example.com/#/filter?catsIds=1&catsIds=2&catsIds=5

will only work if your routing has :

{
  name: 'foo',
  url: '/foo/{arrayParam:int}`,
  params: {
    arrayParam: { array: true }
  }
}

See the doc for more information here : https://ui-router.github.io/ng1/docs/1.0.19/interfaces/params.paramdeclaration.html#array

Comments

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.