2017-03-07 14 views
0

如何處理輸入URL時的情況,例如http://localhost/#!/somepage/,但問題是AngularJS無法處理它並且不加載頁面,否則如果通過somepage去到somepage索引文件正常工作。如何處理Angularjs中的靜態URL定位點

我用ui-router和我的路由腳本的樣子:

angular.module('WEBAPP', ['ui.router', 'bw.paging']) 
    .config(function ($stateProvider, $urlRouterProvider, $locationProvider) { 
     $urlRouterProvider.when('', '/'); 

     var states = [ 
      { 
       name: 'home', 
       url: '/', 
       templateUrl: 'application/partials/home_header.html' 
      }, 

      { 
       name: 'somepage', 
       url: 'somepage/', 
       templateUrl: 'application/partials/somepage.html', 
      }, 

      { 
       name: '404', 
       url: '404/', 
       templateUrl: 'application/partials/404.html' 
      }, 

     ] 

     states.forEach(function (state) { 
      $stateProvider.state(state); 
     }); 
    }); 

如何做正確的路由,我想,當我在瀏覽器中輸入網址http://localhost/#!/somepage/ AngularJS自動重定向到該頁面。

回答

1

嘗試沒有for each.I可能是錯誤的,但我認爲當你在這裏使用每個只有最後一個狀態會綁定$ stateprovider,而不是綁定所有的狀態。像這樣嘗試。

$urlRouterProvider.otherwise('/'); 
$stateProvider 
    .state('home', { 
     url: '/', 
     templateUrl: 'application/partials/home_header.html' 
    }) 
    .state('somepage', { 
     url: 'somepage/', 
     templateUrl: 'application/partials/somepage.html', 
    }) 
    .state('404', { 
     url: '404/', 
     templateUrl: 'application/partials/404.html' 
    }) 

編輯

變化的URL這url: 'somepage/'

這個url: '/somepage'

,當你調用刪除!馬克在url.just http://localhost/#/somepage就夠了。

var states = [ 
      { 
       name: 'home', 
       url: '/', 
       templateUrl: 'application/partials/home_header.html' 
      }, 

      { 
       name: 'somepage', 
       url: '/somepage', 
       templateUrl: 'application/partials/somepage.html', 
      }, 

      { 
       name: '404', 
       url: '/404', 
       templateUrl: 'application/partials/404.html' 
      }, 

     ] 

     states.forEach(function (state) { 
      $stateProvider.state(state); 
     }); 
+0

檢查編輯的答案 –

1

使用此

angular.module('WEBAPP', ['ngRoute']) 
.config(function ($routeProvider) { 
$routeProvider.when('/home', { 
    templateUrl: 'application/partials/home_header.html' 

}).when('/somepage', { 
    templateUrl: 'application/partials/somepage.html' 

}).otherwise({ 
    templateUrl: '/home' 
}); 

});