2016-07-25 29 views
0

我構建了一個小型的網絡應用程序,我使用UI-Router來瀏覽它的頁面。除了瀏覽器刷新或使用狀態鏈接訪問特定頁面之外,一切正常。我嘗試了幾個解決方案發布在同一主題上,但其中沒有一個可能是由於缺少額外信息。如何重新加載瀏覽器並在AngularJS UI-Router中保持當前狀態?

當我點擊下面的ui-sref鏈接時,我會看到'mysite/content'並且頁面顯示正常,但是一旦我刷新它或者嘗試通過粘貼鏈接來訪問它時,就不會找到頁面。

能否請你幫我,使其正常工作。提前致謝。

HTML

<p ui-sref="content">Link To Content</p> 

AngularJS

var app = angular.module('myApp',['ui.router']); 

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouteProvider, $locationProvider){ 


    $urlRouteProvider.otherwise('/'); 


    $stateProvider 

     .state('home', { 
      url: '/', 
      views: { 
       '': { templateUrl: '/app/templates/wrapper.html'}, 
       '[email protected]': { templateUrl: '/app/templates/nav.html'}, 
       '[email protected]': { templateUrl: '/app/templates/header.html'}, 
       '[email protected]': { templateUrl: '/app/templates/footer.html'} 
      } 
     }) 

     .state('content', { 
      url: '/content', 
      views: { 
       '': { templateUrl: '/app/templates/content.html'}, 
       '[email protected]': { templateUrl: '/app/templates/nav.html'}, 
       '[email protected]': { templateUrl: '/app/templates/header.html'}, 
       '[email protected]': { templateUrl: '/app/templates/footer.html'} 

      } 
     }); 

    $locationProvider.html5Mode(true); 


}]) 
+0

你有需要可以將任何數據加載頁面之前得到解決?也許你的應用程序數據沒有加載... –

+0

目前該應用程序是完全空的。現在我注意到,如果將$ locationProvider.html5Mode(true)設置爲false,它將按預期工作,但鏈接中會顯示破折號。試圖解決它 –

回答

0

隨着html5Mode設置爲true,刷新頁面時,瀏覽器會做出 '的mysite /內容' 的請求。在經典的單頁的應用程序,你的後端有一組迴應一些數據路由,它通常被配置成返回應用程序的index.html所有其他途徑(如「mysite的/內容」)。

如果禁用html5Mode因爲瀏覽器使得沒有「#」號,其後端肯定返回預期的index.html後的部分的URL請求它的工作原理。

相關問題