【问题标题】:Redirect from Hash to HashBang using angular使用 Angular 从 Hash 重定向到 HashBang
【发布时间】:2015-12-14 01:43:41
【问题描述】:

我发布了一个网站,使用 mvc c# 和 angular 和 AJAX 调用来创建一些客户端页面。我的一个链接如下所示:

www.website.com/App#/Index

我正在尝试设置一个 hasbang 解决方案以使我的网站可抓取。我成功创建了 hashbang 设置,所以现在工作链接将如下所示:

www.website.com/App#!/Index

现在的问题是我在 Facebook 等上的所有链接都不包含爆炸(!),因此这些链接已经失效。 如何从第一个不带 hashbang 的链接重定向到带 hashbang 的链接?

我目前的路线:

app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(false).hashPrefix('!');
    $routeProvider
      .when('/Index', {
          templateUrl: '/Apps/Modules/IndexPublicAngular/Index.html', controller: 'IndexPublicController'
      });
}]);

【问题讨论】:

  • 有可能吗?

标签: angularjs redirect hashbang


【解决方案1】:

我遇到了一个类似的问题,我需要使用 hashbang 而不仅仅是 hash。 我不得不为此想出一个快速而肮脏的解决方案:

$rootScope.$watch(function () {
    return window.location.hash;
}, function (value) {
    if (value.indexOf('#!#') == 0) {
      setTimeout(function() {
        window.location.hash = value.replace('#!#','#!');
      })
    }
});

那里可能有更好的解决方案,但我没有深入调查,因为这只是书签网址等的临时补丁。请记住,因为这只会在 angular 已经触及 url 后触发,'#'已经被 angular 转换成 '#!#' 了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2017-07-30
    • 2016-05-25
    • 2016-03-23
    • 2020-07-30
    • 2020-03-31
    • 1970-01-01
    相关资源
    最近更新 更多