2016-12-16 48 views
0

我一直在開發一個AngularJS應用程序的最後一個月與angular v1.5.8angular-route v1.5.8node v 7.1.0(與nvm)。突然之間,我的網址包含一個hashbang,路由不起作用,所以我無法更改頁面。AngularJS網址突然包含hashbang

而不是

http://localhost:8080/#/about當我第一次加載頁面時,我得到http://localhost:8080/#!/about

,當我嘗試通過單擊<a href="#/chat">更改爲http://localhost:8080/#/chat我得到http://127.0.0.1:3000/#!/about#%2Fchat

有誰知道爲什麼會發生這種情況,或者我可能改變了這種新行爲?我已經刪除了我的node_modulesnpm install再次編輯,它仍然發生。

相關代碼如下

的index.html

 <ul class="nav navbar-nav navbar-right"> 
     <li><a href="#/about">About</a></li> 
     <li><a href="#/chat">Chat</a></li> 
     </ul> 

routes.js

angular.module('routes', ['ngRoute']) 
.config(['$routeProvider', function($routeProvider){ 
    $routeProvider 
    .when('/about', { 
     templateUrl: 'views/about.html', 
     controller: 'AboutController' 
    }) 
    .when('/chat', { 
     templateUrl: 'views/chat.html', 
     controller: 'ChatController' 
    }) 
    .otherwise({ 
     redirectTo: '/about' 
    }); 
}]); 

回答

0

看看$ locationProvider,其hashPrefix允許你指定你想你的前綴斜線什麼。如果你想讓你的代碼工作,把$ locationProvider放到你的依賴項中並輸入以下內容

$locationProvider.hashPrefix('') 

config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){ 
$locationProvider.hashPrefix('') 
    $routeProvider 
    .when('/about', { 
     templateUrl: 'views/about.html', 
     controller: 'AboutController' 
    })