我怎样才能让angular.js路线长路径路径、路线、angular、js

2023-09-14 00:27:00 作者:寒夜残灯

我正在与angular.js一个文件浏览器。所以,我会处理一些长的URL,

I am making a file explorer with angular.js. so, I will deal with some long url,

例如:

MYDOMAIN /文件夹1 / SUB1 /盛大SUB1 /.././

mydomain/folder1/sub1/grand-sub1/.././

我刚学的角度,并找出角有$ routeProvider,但是,似乎我应该写大量的时,使这项工作(如果我没有定义的模板将不使用时)

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").

不支持的角度*,使所有的子目录的路径使用相同的模板?

does angular support "*" to make all of the sub dir's paths use the same template?

,或做任何其他的方法来处理这​​个问题? THX。

or, does any other method to handle this? Thx.

推荐答案

由于$ routeProvider目前不支持通配符(的在这里看到并在答案2链接),你要攻击它了一点...

Since $routeProvider doesn't currently support wildcards (see here and the 2 links in the answer), you have to hack it up a little...

http://plnkr.co/edit/OuVRSrDUvdVF5yFDHnmM?p=$p$ PVIEW

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