2015-11-10 59 views
2

我使用的是AngularJS路由,我使用了很多技巧,但'#'標記總是出現在URL中。AngularJS路由,從URL中刪除#

請參閱下面的例子/我使用的代碼,但它不會從URL中刪除#號。

angular.module('mod', []). 
    config(['$routeProvider', '**$locationProvider'**, 
      function($routeProvider, **$locationProvider**) { 

    $routeProvider. 
     when('/first', { 
      templateUrl: 'first.html', 
      controller: 'firstCtrl' 
    }); 

    $locationProvider.html5Mode(true); 

    }]); 

有沒有人有解決這個問題?請分享。

回答

1

我想出了相同的問題,並找到了Docs上的解決方案。

$locationProvider.html5Mode({ 
    enabled: true, 
    requireBase: false 
}); 

<head> 
    <base href="/"> 
    ... 
</head> 

如果配置爲使用html5Mode(history.pushState)$的位置,你 需要用 標籤指定應用程序的基本URL或配置$ locationProvider通過 傳遞一個帶有requireBase:false的定義對象的基本標籤不需要 $ locationProvider.html5Mode():

我們做了錯誤的函數調用。試試這個,它會工作。你也可以check the docs

+0

謝謝你,讓我試試這個:) –

2

有任何事情u能做到這一點的工作

你需要指定基本網址與 的應用程序如果您的應用程序的根比的URL不同(例如/我的應用程序內,然後用它作爲你的基地)。

<head> 
    <meta charset="utf-8"> 

    <base href=""> 
</head> 

,或者你可以配置

$locationProvider.html5Mode({ 
    enabled: true, 
    requireBase: false 
});