Cody
Cody

Reputation: 10025

JavaScript | AngularJS | UI-Router: $urlRouterProvider Redirect With Parameters

The Problem...

The Implementer should have the ability to write simple redirects using $urlRouterProvider.when to invoke another state with the same, expected URL-parameters:

$urlRouterProvider.when('/hook/route', '/that/other/route');

This should provide the correct $sateParams needed for the target state. For example, if the URI /hook/route?token=X is called, the target state should have $stateParams.token === 'X' -- given that this parameter is configured within the $stateProvider ({... url: '/that/other/route?token' ...});

The conventional way [in Stackoverflow-land] looks like the following:

$urlRouterProvider
    .when('/hook/route', redirect)
    .otherwise('/');

function redirect($state, $stateParams) {
    $state.go('app.otherState', { token: $stateParams.token });
}

This seems very overkill for a simple redirect-with-parameters action -- especially if many redirects are needed.

The answer I'll provide below shows a very-low-overhead approach, which unfortunately, has not been spot-lighted in other StackOverflow threads regarding this topic...

Upvotes: 0

Views: 1322

Answers (1)

Cody
Cody

Reputation: 10025

... Solution

Match your URI's parameters against those expected by your target state:

$urlRouterProvider
    .when('/hook/route?token', '/route?token')
    .otherwise('/');

Cheers!

Upvotes: 2

Related Questions