Melkor
Melkor

Reputation: 532

How to match optional and undetermined routes in AngularJS routing?

In AngularJS routing, I know a star (*) can match path with slashes, and question mark (?) can match optional path, but how to combine them? I want to match /admin/tasks and /admin/tasks/arbitrary/path/with/slashes together.

I tried this:

    $routeProvider.when('/admin/tasks/:path*?', {
        templateUrl: './templates/admin/task.html',
        controller: 'AdminTaskController'
    })

It can match /admin/tasks/ and /admin/tasks/arbitrary/path/with/slashes, but not with /admin/tasks. It's strange because the doc says:

Route path (matched against $location.path). If $location.path contains redundant trailing slash or is missing one, the route will still match and the $location.path will be updated to add or drop the trailing slash to exactly match the route definition.

Why can't the route match URL without the trailing slash?

Besides, in the doc,

path can contain optional named groups with a question mark: e.g.:name?.

But actually using ? alone cannot match path with slashes.

Upvotes: 1

Views: 92

Answers (1)

ierdna
ierdna

Reputation: 6293

i solved it by duplication. it's not ideal, but it works:

.when('/system/:path?',{
    templateUrl:'assets/templates/pages/system.html',
    controller: "SystemController",
    controllerAs: "systemCtrl"
})
.when('/system/:path*',{
    templateUrl:'assets/templates/pages/system.html',
    controller: "SystemController",
    controllerAs: "systemCtrl"
});

now when people type in http://localhost/system/files/15/delete it recognizes it and if they just go to http://localhost/system

Upvotes: 0

Related Questions