smstromb
smstromb

Reputation: 596

Angularjs UI-Router: Incorrectly interpreting url path as paremeter in $urlRouterProvider

I have the following setup in my $stateProvider:

$stateProvider
    .state('main.newworkspace', {
        url: "/workspace/new",
        templateUrl: "views/main/newworkspace.html",
        controller: "NewWorkspaceCtrl"
    })
    .state('main.workspace', {
        url: "/workspace/:id",
        templateUrl: "views/main/workspace/main.html",
        controller: "WorkspaceCtrl",
        abstract: true
    })
    .state('main.workspace.dashboard', {
        url: "/dashboard",
        templateUrl: "views/main/workspace/dashboard.html",
        controller: "DashboardCtrl"
    })
    .state('main.workspace.info', {
        url: "/info",
        templateUrl: "views/main/workspace/info.html",
        controller: "InfoCtrl"
    })

The following routes are now enabled for this app:

/workspace/new
/workspace/:id
/workspace/:id/dashboard
/workspace/:id/info

Because /workspace/:id is abstract, I would like it to automatically redirect to /workspace/:id/dashboard. I attempted to accomplish this by adding a when() to $UrlRouterProvider:

$urlRouterProvider.when('/workspace/:id', '/workspace/:id/dashboard');

So now it correctly redirects

/workspace/:id ------> /workspace/:id/dashboard

But also redirects /workspace/new

/workspace/new ------> /workspace/new/dashboard

Is there a quick fix for this? I also have tried determining if $stateParam.id == 'new' in the $urlRouterProvider.when(...) function call but $stateParam and $state.params are always empty.

Upvotes: 3

Views: 1011

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

We should be able to give the special route priority similar way:

// when for the NEW state... will be matching first - and used
$urlRouterProvider.when('/workspace/new'
                       , function($state){$state.go('main.newworkspace');})
// other 'when' as needed...
$urlRouterProvider.when('/workspace/:id', '/workspace/:id/dashboard');

The first resloved .when() will win...

Upvotes: 3

Related Questions