AngularJS路由,从URL中删除#

AngularJS routing, remove # from the URL

我正在使用 AngularJS 路由并且我使用了很多技巧,但是 '#' 符号总是出现在 URL。

参考下面的 Example/Code,我使用了它,但它没有从 URL 中删除 # 符号。

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

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

   $locationProvider.html5Mode(true);

  }]);

有人对此有解决方案吗?请分享。

我遇到了同样的问题,并在文档中找到了解决方案。

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

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

If you configure $location to use html5Mode (history.pushState), you need to specify the base URL for the application with a tag or configure $locationProvider to not require a base tag by passing a definition object with requireBase:false to $locationProvider.html5Mode():

我们调用了错误的函数。试试这个,它会起作用。你也可以check the docs

你可以做任何事情

您需要为应用程序指定基础 URL 如果您的应用程序根目录不同于 url(例如 /my-app,则使用它作为您的根目录)。

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

    <base href="">  
</head>

或者您可以配置

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