Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass complex object to ui-sref params

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?

like image 381
Karmazzin Avatar asked Dec 25 '14 12:12

Karmazzin


1 Answers

The UI-Router is now shipped with support for custom types of params. There is updated and working version of your plunker.

So, we can adjust the state def like this:

app.config(function($stateProvider) {
  $stateProvider.state('list', {
    url: 'list?{filter:CoolParam}',

As we can see, the filter is now of type CoolParam. Here we will define it:

app.config(['$urlMatcherFactoryProvider', function($urlMatcherFactory) {

  $urlMatcherFactory.type('CoolParam',
  {
     name : 'CoolParam',
     decode: function(val)  { return typeof(val) ==="string" ? JSON.parse(val) : val;},
     encode: function(val)  { return JSON.stringify(val); },
     equals: function(a, b) { return this.is(a) && this.is(b) 
                                  && a.status === b.status && a.type == b.type },
     is: function(val)      { return angular.isObject(val) 
                                  && "status" in val && "type" in val },
  })

}]);

And now the {{$stateParams}} of a link like this:

<a ui-sref="list({filter: {status: 1, type:2}})">Click me to see params</a>

will return:

{"filter":{"status":1,"type":2}}

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:

#/list?filter=%7B%22status%22:1,%22type%22:2%7D

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:

  • Angular ui router parse query params into booleans
  • What is the correct way to use the “bool” param type with ui-router?

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 type

$stateProvider.state('list2', {
    url: 'list2?state&type',
})

But in this case we would have it like this ?status=1&type=2. This mapping is also part of this plunker.

like image 110
Radim Köhler Avatar answered Oct 24 '22 22:10

Radim Köhler