2016-03-11 86 views
0

我用$locationProvider從這個url

http://localhost/angular-phonecat/app/#/phones 

刪除hashtag #使用下面的代碼刪除散列#:

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

    $routeProvider. 
    when('/phones', {templateUrl: 'partials/phone-list.html', controller: 'PhoneListCtrl'}). 
    when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: 'PhoneDetailCtrl'}). 
    otherwise({redirectTo: '/phones'}); 

    $locationProvider.html5Mode(true); 

}]); 

index.html加入這頭

<base href="/angular-phonecat/app/"> 

這個工作和我的當前網址是:

http://localhost/angular-phonecat/app/phones 

但是,當我直接訪問該url它沒有顯示,因爲它不調用index.html文件的網頁。相反,它顯示/app/phones目錄的內容

enter image description here

我試圖通過創建一個.htaccess文件還

RewriteEngine On 
RewriteBase /
RewriteCond  %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico) 
RewriteCond  %{REQUEST_FILENAME} !-f 
RewriteCond  %{REQUEST_FILENAME} !-d 
RewriteRule  ./index.html [L] 
+0

HTTPS編輯迴應: //scotch.io/tutorials/pretty-urls-in-angularjs-removing-the-hashtag 嘗試以上內容 –

回答

1

。在你的規則的一個小錯誤,重寫規則需要一個參數。

此外,當您編寫RewriteCond %{REQUEST_FILENAME} !-d時,如果存在具有該名稱的目錄,則會阻止重定向。由於/anular-phonecat/app/phones存在,您還需要刪除此行。

你不需要的RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)條件,因爲文件不會被redirecte感謝RewriteCond %{REQUEST_FILENAME} !-f條件

結果是:

RewriteEngine On 
RewriteBase  /angular-phonecat/app/ 
RewriteCond  %{REQUEST_FILENAME} !-f 
RewriteRule  .* ./index.html [L] 

基於評論

+0

仍然無法正常工作 – Adersh

+0

你的應用程序是服務器的根級別嗎? (例如http://localhost/index.html)?什麼是錯誤? –

+0

不,網址是http://localhost/angular-phonecat/app/index.html – Adersh

相關問題