2013-07-10 46 views
5

我正在处理AngularJS中的文件编辑应用程序。我的网址是这样的:AngularJS全通路线?

#/ fileName.md

#/文件夹/ fileName.md

#/文件夹/嵌套文件夹/另一-FOL DER/itgoesonforever/filename.MD

我不想必须为每一个做深度的路线,它可能是〜15条航线深。有什么方法可以有条件的路线吗?粗略地:

/:fileorfolder?/:fileorfolder?/:fileorfolder?/:fileorfolder? 
+0

像*? (fi – Ven

+0

对不起?您能解释一下吗? – wesbos

+0

我从来没有搞错Angular的路由,但是我已经读过了Angular UI项目的路由器:https://github.com/angular-ui/ui-router是否满足您的需求? –

回答

15

我认为你可以用角做的最好的是*,这是新的一样的v1.1.5 of $routeProvider

path can cont ain以星号开头(​​)。当路线匹配时,所有字符都以指定名称热切存储在$routeParams中。
例如,像/color/:color/largecode/*largecode/edit路线将匹配/color/brown/largecode/code/with/slashes/edit和提取:
- color: brown
- largecode: code/with/slashes

你不得不解析largecode PARAM自己虽然。

+0

哇,正是我想要的。在事实完全通过后,我甚至不必进行任何奇怪的连接作为一个参数,谢谢! – wesbos

+2

现在已经在更新版本的angular中改成':name *'了。 –

6

我想我明白了!诀窍是将模板设置为简单模式,然后修改范围以包含模板的动态路径。

所以,现在我可以放置一个文件在/foo/bar/baz.html并查看模板呈现通过去server.com/foo/bar/baz

// Routes 
app.config(function($routeProvider) { 
    $routeProvider 

    // Home 
    .when('/', { 
     templateUrl: 'home.html', 
     controller: 'HomeController' 
    }) 

    // Catch All 
    .when('/:templatePath*', { 
     template: '<ng-include src="templatePath"></ng-include>', 
     controller: 'CatchAllCtrl' 
    }) 

}); 

// Catch All Controller 
app.controller("CatchAllCtrl", function($scope, $routeParams) { 
    $scope.templatePath = $routeParams.templatePath + '.html'; 
});