简体   繁体   English

如何使angular.js路由长路径

[英]How can I make the angular.js route the long path

I am making a file explorer with angular.js. 我正在使用angular.js创建一个文件浏览器。 so, I will deal with some long url, 所以,我会处理一些长网址,

eg: 例如:

mydomain/folder1/sub1/grand-sub1/.././ MYDOMAIN /文件夹1 / SUB1 /盛大SUB1 /.././

I just learn angular and find out that angular has the $routeProvider, but, it seems I should write lots of "when" to make this work (the template will not used if I don't define the "when"). 我只是学习角度,并发现angular有$ routeProvider,但是,似乎我应该写很多“when”来使这个工作(如果我没有定义“when”,模板将不会被使用)。

does angular support "*" to make all of the sub dir's paths use the same template? 角度支持“*”使所有子目录的路径使用相同的模板?

or, does any other method to handle this? 或者,有没有其他方法来处理这个? Thx. 谢谢。

Since $routeProvider doesn't currently support wildcards ( see here and the 2 links in the answer), you have to hack it up a little... 由于$ routeProvider目前不支持通配符( 请参阅此处和答案中的2个链接),您必须将其破解一点......

http://plnkr.co/edit/OuVRSrDUvdVF5yFDHnmM?p=preview http://plnkr.co/edit/OuVRSrDUvdVF5yFDHnmM?p=preview

HTML HTML

<a href="#/dir">/</a>
<br/>
<a href="#/dir/folder1">/folder1</a>
<br/>
<a href="#/dir/folder1/sub1">/folder1/sub1</a>
<br/>
<a href="#/dir/folder1/sub1/grandsub1">/folder1/sub1/grandsub1</a>
<br/>

JavaScript JavaScript的

app.controller('DirCtrl', function ($scope, $route) {
  var p = $route.current.params;

  $scope.path = '/';
  if (p.p1) $scope.path += p.p1;
  if (p.p2) $scope.path += '/' + p.p2;
  if (p.p3) $scope.path += '/' + p.p3;
  if (p.p4) $scope.path += '/' + p.p4;
  if (p.p5) $scope.path += '/' + p.p5;
});

app.config(function ($routeProvider) {
  $routeProvider
    .when('/', {templateUrl: 'home.html', controller: 'HomeCtrl'})
    .when('/dir', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .when('/dir/:p1', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .when('/dir/:p1/:p2', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .when('/dir/:p1/:p2/:p3', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .when('/dir/:p1/:p2/:p3/:p4', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .when('/dir/:p1/:p2/:p3/:p4/:p5', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    /* add more as necessary */
    .otherwise({redirectTo: '/'});
});

Don't repeat yourself! 不要重复自己!

var dirRoute = {templateUrl: 'dir.html', controller: 'DirCtrl'};

$routeProvider
  .when('/', {templateUrl: 'home.html', controller: 'HomeCtrl'})
  .when('/dir', dirRoute)
  .when('/dir/:p1', dirRoute)
  .when('/dir/:p1/:p2', dirRoute)
  .when('/dir/:p1/:p2/:p3', dirRoute)
  .when('/dir/:p1/:p2/:p3/:p4', dirRoute)
  .when('/dir/:p1/:p2/:p3/:p4/:p5', dirRoute)

Starting in AngularJS 1.2 (currently in release candidate phase), you can use wildcards to match more of a path. 从AngularJS 1.2开始(当前处于候选发布阶段),您可以使用通配符来匹配更多路径。 From the new documentation : 从新文档

path can contain named groups starting with a colon and ending with a star ( :name* ). path可以包含以冒号开头并以星号结尾的命名组( :name* )。 All characters are eagerly stored in $routeParams under the given name when the route matches. 当路由匹配时,所有字符都急切地存储在给定名称下的$routeParams中。

For example, routes like /color/:color/largecode/:largecode*\\/edit will match /color/brown/largecode/code/with/slashs/edit and extract: 例如, /color/:color/largecode/:largecode*\\/edit将匹配/color/brown/largecode/code/with/slashs/edit和extract:

 color: brown largecode: code/with/slashs. 

It's worth nothing that you now have to include the extra angular-route.js file, as the routing layer is no longer included by default to save file size. 您现在必须包含额外的angular-route.js文件,因为默认情况下不再包含路由图层以保存文件大小。 Furthermore, your module must declare ngRoute as one of its dependencies. 此外,您的模块必须将ngRoute声明为其依赖项之一。 Your code would look like this: 您的代码如下所示:

var app = angular.module('app', ['ngRoute']);

app.controller('HomeCtrl', function ($scope, $route) {
});

app.controller('DirCtrl', function ($scope, $routeParams) {
  $scope.path = '/' + $routeParams.dir;
});

app.config(function ($routeProvider) {
  $routeProvider
    .when('/', {templateUrl: 'home.html', controller: 'HomeCtrl'})
    .when('/dir/:dir*', {templateUrl: 'dir.html', controller: 'DirCtrl'})
    .otherwise({redirectTo: '/'});
});

Working example: http://plnkr.co/edit/BSPWYPnHM7GJRgNCAjoi?p=preview 工作示例: http//plnkr.co/edit/BSPWYPnHM7GJRgNCAjoi?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM