2014-06-06 22 views
5

請考慮以下來自ui-router wiki的稍微修改過的代碼。

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

myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) { 
    // for any unmatched url 
    $urlRouterProvider.otherwise("/state1"); 

    $locationProvider.html5Mode(true); 

    // now set up the states 
    $stateProvider 
     .state('state1', { 
      url: '/state1', 
      templateUrl: "partials/state1.html" 
     }) 
     .state('state1.list', { 
      url: "/list", 
      templateUrl: "partials/state1.list.html", 
      controller: function($scope) { 
       $scope.items = ["A", "List", "of", "Items"]; 
      } 
     }) 
     .state('state2', { 
      url: "/state2", 
      templateUrl: "partials/state2.list.html", 
      controller: function($scope) { 
       $scope.things = ["a", "set", "of", "things"]; 
      } 
     }) 
}); 

執行Python 3的SimpleHttpServer,我得到一個404 error試圖訪問時:http://localhost:8000/state1234324324

爲什麼沒有$urlRouterProvider.otherwise("/state1");重新定向所有未知的路線/state1,其每本question,先後與/state1 URL相關聯的規定state

回答

9

這是因爲當你用像「/ state123413」這樣的URL打開服務器時,它會直接返回404響應(客戶端沒有路由)。

你需要做的是有一個catchall路線。例如在快遞中你可以做

app.get('/*', function(req, res){ 
    res.render('defaulttemplate') 
} 

這將強制客戶端的路由。請參閱ui路由器faq以獲取常用服務器以設置catchall路由。

+0

爲什麼這不適用於ui路由器?我的路線不工作,因爲我補充說,修復,雖然否則現在工作... –

相關問題