2014-09-13 36 views
0

我想有我的路線,如堆棧溢出。所以如果我瀏覽到/category/id/服務器將重定向到/category/id/name。但是,我不能讓angularjs路由在包含斜槓時拾取我的路由。routeProvider沒有撿起路線AngularJS

所以這是我secnarios:

localhost:81/butwhy => works  
localhost:81/category/id => redirects to localhost:81/category/fail  
localhost:81/category/id/name => redirects to localhost:81/category/id/fail 

的routeProvider似乎只拿起最後一個斜線之後的部分。爲什麼?

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 

    $routeProvider. 
     when('/category/:id/:name', { 
      templateUrl: '/view/example.html', 
      controller: 'ExampleCtrl' 
     }). 
     when('/category/:id', { 
      templateUrl: '/view/example.html', 
      controller: 'ExampleCtrl' 
     }). 
     when('/butwhy', { 
      // just for testing 
     }). 
     when('/', { 
      templateUrl: '/view/examples.html', 
      controller: 'ExamplesCtrl' 
     }).  
     otherwise({ 
      redirectTo: 'fail' 
     }); 

    $locationProvider.html5Mode(true); 
}]); 

我不知道它的問題,但我使用CakePHP與WAMP和angularjs版本1.2.23

編輯:

我在/ sitefolder /根目錄下的.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ index.php [QSA,L] 
</IfModule> 

並在/ sitefolder /的.htaccess

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^$ webroot/ [L] 
    RewriteRule (.*) webroot/$1 [L] 
</IfModule> 

cakephp的意見是在/ sitefolder/view /和角度的html視圖是在/ sitefolder/webroot/view/

+0

有你設置的htaccess與html5mode工作?如果沒有,你需要,如果有的話不對 – charlietfl 2014-09-13 18:43:50

+0

謝謝charlietfl,這可能是。至少我知道現在要搜索什麼 – 2014-09-13 19:37:30

+0

我改變了主意。我不認爲它是htaccess,因爲路由在服務器上工作,並且使用angularjs設置返回佈局。那麼爲什麼會是id?我昨天測試了幾個不同的htaccess配置,但沒有成功 – 2014-09-14 10:45:57

回答

1

我從來沒有得到它與CakePHP的服務佈局工作。但是我發現你可以在ui-router中嵌套視圖。所以我不需要通過cakephp來服務佈局了。

與常規index.html作爲在根目錄佈局,並通過/ AJAX所有Ajax調用/名我安裝了的.htaccess這樣的:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteRule ^ajax index.php [NE,L] 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.html [NE,L] 
</IfModule> 

但這並沒有當我直接瀏覽工作localhost:81,那麼它使用index.php而不是index.html。它工作時,我雖然在哈德路徑。 對此問題進行修復是改變在httpd.conf順序,這樣的index.html是的index.php之前

<IfModule dir_module> 
    DirectoryIndex index.html index.php index.php3 index.htm 
</IfModule> 
-1

我認爲你缺少#,請嘗試以下操作。

URL應該

本地主機:81 /#/分類/ ID
本地主機:81 /#/分類/ ID /名稱

+1

html5mode刪除了hashbang。 – Shomz 2014-09-13 19:30:19