I need build url like this: /list?filter[status]=1&filter[type]=2
I do:
link:
<a ui-sref="list({filter: {status: 1, type:2}})">List</a>
(pass complex object in params, if pass simple object - {filter: 1} - it ok, but I need this)
state:
.state('list', {
url: '/list?filter',
…
})
In total I get url like:
/list?filter=[object Object]
Demo: http://plnkr.co/edit/wV3ieKyc5WGnjqw42p7y?p=preview
How I can fix it?
The
UI-Router
is now shipped with support forcustom types
of params. There is updated and working version of your plunker.So, we can adjust the state def like this:
As we can see, the filter is now of type
CoolParam
. Here we will define it:And now the
{{$stateParams}}
of a link like this:will return:
NOTE: In this case I made my life easier, and simply converted json into string. This means, that url encoded param will look like this:
which is
{"status":1,"type":2}
But we can provide also other ways how to express our filter object
Check it here
Also related Q & A:
So, the above solution is nicely working with filter as a JSON. But in case that we must have url like this
?filter[status]=1&filter[type]=2
, we have to define the state differently. Each parameter must be declared as a separated simple typeBut in this case we would have it like this
?status=1&type=2
. This mapping is also part of this plunker.