角度路由 IE9 错误



在IE9中,我的角度路由不起作用,它将我重定向到"#"标签之前的任何内容。

mysite.com/#/info -> mysite.com

即使我手动从 URL 中删除/# 并重试,我仍然会被重定向。

它将"#"附加到我的 URL 中,因为 html5 模式在浏览器支持时使用历史记录 API,并在不支持时回退到"hashbang"(#)(如 IE9)。

$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise("/");
$stateProvider
  .state('statistics', {
      url: "/path/:myId",
      templateUrl: '../some/path/site.html',
      controller: 'Ctrl'
  }
);

一直在寻找各种解决方案,例如:

  • AngularJS 如何使用路由删除 IE9 中的 # 符号

  • https://gist.github.com/thomseddon/3834721

但他们都不适合我。

我正在寻找一种无需停用 html5mode 的解决方案。有人遇到过类似的问题并设法解决吗?

在 HTML5 模式下,有三种情况不会重写 A 标签: 从角度文档 - 1 包含目标属性的链接。示例:链接 2 指向不同域的绝对链接 示例:链接 3 以"/"开头的链接,在定义 base 时指向不同的基本路径 示例:链接

您可以尝试使用这样的全局指令,将 target='_self' 添加到所有没有目标的链接中。

myApp.directive('a', [function () {
return {
    restrict: 'E',
    link: function (scope, element, attrs) {
        if (!attrs['target']) {
            element.attr('target', '_self');
        }
    }
}
}]);

最新更新