【问题标题】:angular ui routing - avoid hashbang mode. ie9角度 ui 路由 - 避免 hashbang 模式。即9
【发布时间】:2016-04-28 12:21:19
【问题描述】:

我正在尝试让 html5mode,角度 ui 路由在 IE9 中工作

if (window.history && window.history.pushState) {
  $locationProvider.html5Mode(true);
} else {
    console.log('IE9');
    window.location.hash = '/';  // IE 9 FIX            
    $locationProvider.html5Mode(true);
}

$urlRouterProvider.otherwise("/");

$stateProvider
  .state('placeholder', {
      url: "/path/:myId",
      templateUrl: '../path.html
      controller: 'ResultCtrl'
   }
 );

我仍然得到 index/#/index/myid 而不是 index/path/myid,因为浏览器会忽略哈希标记及其之后的所有内容,所以它会带我进入索引。 (适用于 firefox、chrome、edge、IE11-10(支持 html5mode / api 历史的浏览器))

【问题讨论】:

  • 您是否指定了应用程序的基本网址?
  • @MaKCbIMKo 是的,我的头部有基本网址。
  • @DrColossos 不是真的,我被重定向到主题标签之前的任何内容,而不是空白页面。

标签: angularjs internet-explorer angular-ui-router angular-routing


【解决方案1】:

除了使用 UI 路由器之外,我的应用中还有以下内容:

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

我在 app/scripts/app.js 文件中的配置函数中添加了以下代码

在config函数中加入如下代码:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2014-11-03
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多