在 AngularJS 中递归路由动态路径 [英] Routing dynamic Paths recursively in AngularJS

查看:24
本文介绍了在 AngularJS 中递归路由动态路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 AngularJS 构建类似目录浏览器的东西.是否可以使用 ng-route 路由路径?我想解析这样的 URL:myapp.com/#/folder1/folder2/.../folderN

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

推荐答案

来自 $routeProvider文档

path 可以包含以冒号开头并以 a 结尾的命名组明星:例如:姓名*.所有字符都急切地存储在 $routeParams 中路由匹配时在给定名称下.

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.

所以你可以定义

$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('/');
});

演示 JSFIDDLE:http://jsfiddle.net/rYm5G/

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

这篇关于在 AngularJS 中递归路由动态路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆