为什么AngularJS会重写hashbang

时间:2015-09-09 12:58:27

标签: angularjs

当我输入

时,为什么AngularJS会重写地址栏
www.domain.com/index.html/#!/content/sub/content 

www.domain.com/index.html#/!/content/sub/content

/#/!不是标准的hashbang语法..我想将它保留为#!/ content / subcontent,然后用自定义函数解析url。

我已经尝试了

$location.html5Mode = false;

angular.module('locationModule', [])

    .config(function($routeProvider, $locationProvider) {
        //$locationProvider.hashPrefix('!');
        $locationProvider.html5Mode = false;
    });

这两者似乎都没什么区别。

2 个答案:

答案 0 :(得分:1)

默认情况下禁用HTML5模式。如果您不想要/#/.

,则此前缀由用户设置

$locationProvider.html5Mode(false).hashPrefix('!');

答案 1 :(得分:0)

$ locationProvider是Angular核心的一部分,用于配置应用程序的路径。默认情况下,Angular使用#!作为路径前缀,称为Hashbang模式。这是一种表示JavaScript会触发页面加载的约定。

我将以下代码添加到app / scripts / app.js文件中的配置功能

将以下代码添加到配置功能:

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

通过将html5Mode方法的enabled属性设置为true,禁用hashbang URL;也就是说,用户将看到没有hashbang的干净URL。将requireBase属性设置为false与hashbang问题无关,但这是避免常见$ location错误的一种方法。