使用angular从Hash重定向到HashBang [英] Redirect from Hash to HashBang using angular

查看:48
本文介绍了使用angular从Hash重定向到HashBang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了一个网站,该网站使用mvc c#进行了angular和AJAX调用,以创建一些客户端页面.我的链接之一如下所示:

I have published a website using mvc c# with angular and AJAX calls to create some client side pages. One of my links look like this:

www.website.com/App#/Index

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

I am trying to setup a hasbang solution to make my site crawlable. I succesfully created the hashbang setup so now a working link will look like this:

www.website.com/App#!/Index

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

The issue now is that all my links out on Facebook and so on does not contain the bang (!) and therefore those links are dead. How do I redirect from the first link without hashbang to the link with hashbang?

我当前的路线:

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

推荐答案

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

I hit a similar issue that you had where I need to use hashbang instead of just hash. I had to come up with a quick and dirty solution for this:

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

可能有更好的解决方案,但是我没有进行深入研究,因为这只是书签网址等的临时补丁.请记住,因为只有在角度已经触及网址#"后才会触发已经通过角度转换为#!#".

There is probably a better solution out there, but I didn't investigate very deeply since this is only a temporary patch for bookmarked urls etc. Keep in mind since this only fires after angular has already touched the url, '#' is already converted into '#!#' by angular.

这篇关于使用angular从Hash重定向到HashBang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆