LinusK
LinusK

Reputation: 337

Routing dynamic Paths recursively in AngularJS

I want to build something like a directory browser with AngularJS. Is is possible to route paths with ng-route? I'd like to parse URLs like this: myapp.com/#/folder1/folder2/.../folderN

Upvotes: 1

Views: 855

Answers (1)

Raghavendra
Raghavendra

Reputation: 5387

From $routeProvider docs

path can contain named groups starting with a colon and ending with a star: e.g.:name*. All characters are eagerly stored in $routeParams under the given name when the route matches.

So you could define

$routeProvider.when('/:folders*', {
    template:'template'
})

And then (in controller e.g.)

$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
    if (angular.isDefined($routeParams.folders))
        var foldersArray = $routeParams.folders.split('/');
});

DEMO JSFIDDLE: http://jsfiddle.net/rYm5G/

Upvotes: 2

Related Questions