问题
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
回答1:
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/
来源:https://stackoverflow.com/questions/22985259/routing-dynamic-paths-recursively-in-angularjs