Kindzoku
Kindzoku

Reputation: 1420

ui-router change url after $state.go

I have a route:

$stateProvider.state('keeper.telepay.category', {
    url: '/category-{id}/country-{cid}/region-{rid}'
}

In all cases when I use $state.go with this route, I want to change rid to 'all' if it's value is '0', so my url'll be like

/category-1/country-195/region-all

I've tried to do it in $rootScope.$on('$stateChangeStart'), but with no luck. Any suggestions || directions would be very appreciated.

UPDATE

Here what I've tried on $stateChange:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if(toState.name == 'keeper.telepay.category' && toState.params.rid == 0) {
        toState.params.rid = 'all';
    }
}

Upvotes: 0

Views: 850

Answers (1)

Kindzoku
Kindzoku

Reputation: 1420

Accidently I've stumbled to this link http://angular-ui.github.io/ui-router/feature-1.0/interfaces/params.paramdeclaration.html#squash

So the pure solution is:

$stateProvider.state('keeper.telepay.category', {
    url: '/category-{id}/country-{cid}/region-{rid}',
    params: {
        cid: '195',
        rid: {
            value: '0',
            squash: "all" <-- do the trick
        }
    }
});

And here is description:

Configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value.

There are three squash settings:

false: The parameter's default value is not squashed. It is encoded and included in the URL

true: The parameter's default value is omitted from the URL. If the parameter is preceeded and followed by slashes in the state's url declaration, then one of those slashes are omitted. This can allow for cleaner looking URLs.

"arbitrary string": The parameter's default value is replaced with an arbitrary placeholder of your choice.

Upvotes: 1

Related Questions