Routing dynamic Paths recursively in AngularJS

拜拜、爱过 提交于 2019-12-24 14:02:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!