2016-01-21 94 views
0

當我正確使用html5mode時,出現$routeProvider問題。 當我訪問/profile/: child它會沒事的,但是當頁面刷新的時候,那麼在頁面上會出現錯誤。是有什麼毛病我下面的代碼:

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

var pathViews = function (file) { 
    return 'http://localhost/mycompany/app/views/' + file; 
}; 
var pathError = function (file) { 
    return 'app/views/error/' + file; 
}; 
$locationProvider.html5Mode(true); 
$routeProvider 
    .when('/', { 
     templateUrl: pathViews('dashboard.html'), 
     controller :'MainCtrl' 
    }) 
    .when('/home', { 
     templateUrl: pathViews('dashboard.html'), 
     controller :'MainCtrl' 
    }) 
    .when('/profile/:child', { 
     templateUrl: function($routeParams) { 
      return pathViews($routeParams.child+'.html')}, 
       controller:'MainCtrl' 
    }) 
    .when('/404', { 
     templateUrl: pathError('404.html'), 
     controller :'MainCtrl' 
    }) 
    .when('/form-standard', { 
     templateUrl: pathViews('form-standard.html'), 
     controller :'MainCtrl' 
    }) 
    .otherwise({redirectTo: '/404'});}); 

和index.html的是: <div ng-view ></div>

+3

你能解釋一下錯誤是什麼嗎?如果控制檯日誌中有任何輸出,你能解釋它嗎? – GillesC

+0

我沒有登錄瀏覽器,瀏覽器就像沒有響應.. –

+0

沒有響應,因爲在繼續加載,或加載空白頁面,更多細節? – Yerken

回答

1

這是正常的,因爲當你刷新頁面Web服務器嘗試加載沒有按」所請求的資源t存在於服務器上,而是在客戶端處理。

例如,如果部署一個「routeProvided」上一個Apache angularjs應用程序,你應該修改你的.htaccesssource):

RewriteEngine on 

# Don't rewrite files or directories 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 

# Rewrite everything else to index.html to allow html5 state links 
RewriteRule^index.html [L] 

在簡單的英語,你要告訴您的Web服務器重定向每請求到您的主要模塊入口點。

+0

我試圖使用.htaccess,它是。但沒有解決 –