如何在Angularjs中将哈希值更改为斜杠

时间:2014-04-18 08:48:16

标签: angularjs

  <script type="text/javascript">

angular.module('ngView', ['ngRoute'],
    function($routeProvider){
      $routeProvider.when('/test',
        {
          template: '{{}}',
          controller: function(){
            console.log('controller');
            $('div').first().html('<b>OK</b>');
          }
        }
      );
    }
  );
  </script>

这是我的所有角度代码。在URL中,我可以通过

访问测试
file:///Users/tom/Documents/Projects/a4/index2.html#/test
file:///Users/tom/Documents/Projects/a4/index2.html#test

以上两种方式都有效。

但是当我更改#/test to /test时,它无法正常工作。

如何将Angularjs配置为仅在URL路径中使用'/'

1 个答案:

答案 0 :(得分:2)

我发现您在本地工作,需要一台服务器才能使用html5Mode,启用它后,您需要在服务器上定义.htaccessvirtual host看看在这个问题AngularJS: can't get html5 mode urls with ui-route $state

<VirtualHost *:80>
    ServerName my-app

    DocumentRoot /path/to/app

    <Directory /path/to/app>
        RewriteEngine on

        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]

        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
    </Directory>
</VirtualHost>