当我点击后退按钮时,它会将我带到我当前所在的同一页面

时间:2014-10-19 16:48:51

标签: javascript jquery angularjs browser browser-history

我正在查看由其他人开发的网站,并且我正在尝试解决后退按钮无法正常运行的问题。由于这不是我的代码,因此我不确定在哪个方面优先处理我的问题排查方式。

我试图快速解决这个问题 - 我的问题是:有什么常见的事情会导致这种情况发生?有哪些方面需要检查,很可能是罪魁祸首?

这是行动中的错误。如果您点击任何示例项目组合项目,然后尝试使用浏览器自己的后退按钮,您将被带回同一页面。我相信这是Angular中的路由问题,但我不确定。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

要修复后退按钮问题,您应该将所有href指向#/ route_name而不是html中的#route_name 例如改变:

<a href="#about"  ....   </a>

要:

<a href="#/about"   ....      </a>

我在您的网站上测试了该解决方案,后退按钮就像一个魅力。

答案 1 :(得分:1)

我不太了解angular,但行为可能是因为compiled.min.js文件中的以下代码。 此代码段中的最后4-5行非常重要,这可能会导致此问题。看看它,可能你可以搞清楚:

angular.module("Site", ['ngSanitize']).config(function ($locationProvider, $routeProvider, $httpProvider) {

    $httpProvider.defaults.useXDomain = !0, delete $httpProvider.defaults.headers.common["X-Requested-With"], $locationProvider.html5Mode(false);
    $httpProvider.responseInterceptors.push(function ($q, $location, $rootScope) {
        return function (promise) {
            //start spinner
            /*  $rootScope.element = $('.container')
            $rootScope.element.css('visibility', 'hidden');
            $rootScope.spinner = $rootScope.spinner ? $rootScope.spinner : startSpinner(); */
            return promise.then(
            // Success: just return the response
            function (response) {
                return response;
            },
            // Error: check the error status to get only the 401
            function (response) {
                if (response.status === 404) $location.url('/404');
                return $q.reject(response);
            });
        }
    });

    $routeProvider.when("/", {
        templateUrl: "views/homepage.html",
        controller: "homeC"
    }).when("/about", {
        templateUrl: "views/about.html",
        controller: "RouteC"
    }).when("/about/our-team", {
        templateUrl: "views/our-team.html",
        controller: "aboutC"
    }).when("/who-we-are", {
        templateUrl: "views/page.html",
        controller: "RouteC"
    }).when("/what-we-do", {
        templateUrl: "views/page.html",
        controller: "RouteC"
    }).when("/about/manifesto", {
        templateUrl: "views/manifesto.html",
        controller: "RouteC"
    }).when("/about/testimonials", {
        templateUrl: "views/testimonials.html",
        controller: "testimonialC"
    }).when("/about/awards", {
        templateUrl: "views/awards.html",
        controller: "RouteC"
    }).when("/services", {
        redirectTo: "/services/design"
    }).when("/services/marketing", {
        templateUrl: "views/services-marketing.html",
        controller: "services_marketing"
    }).when("/services/design", {
        templateUrl: "views/services-design.html",
        controller: "services_design"
    }).when("/services/ecommerce", {
        templateUrl: "views/services-ecommerce.html",
        controller: "services_ecommerce"
    }).when("/services/development", {
        templateUrl: "views/services-development.html",
        controller: "services_development"
    }).when("/services/mobile", {
        templateUrl: "views/services-mobile.html",
        controller: "services_mobile"
    }).when("/services/marketing/:slug", {
        templateUrl: "views/services.html",
        controller: "services_marketing_internal"
    }).when("/services/:slug", {
        templateUrl: "views/services.html",
        controller: "services_internal"
    }).when("/portfolio", {
        redirectTo: "/portfolio/website"
    }).when("/portfolio/:slug", {
        templateUrl: "views/portfolio.php",
        controller: "portfolioC"
    }).when("/portfolio/:category/:slug", {
        templateUrl: "views/portfolio-single.php",
        controller: "portfolio_internal"
    }).when("/blog-home", {
        templateUrl: "views/blog-home.html",
        controller: "RouteC"
    }).when("/blog-post", {
        templateUrl: "views/blog-post.html",
        controller: "RouteC"
    }).when("/contact", {
        templateUrl: "views/contact.php",
        controller: "contactC"
    }).when("/debug", {
        templateUrl: "views/qunit.html",
        controller: "qunitC"
    }).when("/404", {
        templateUrl: "views/error-404.html"
    }).when("/sitemap", {
        templateUrl: "views/sitemap.php"
    }).otherwise({
        redirectTo: "/404"
    });

    //$locationProvider.hashPrefix('!'); //This seems to be the code which handles the redirect.
}).run(function ($rootScope, $location) {
    $rootScope.$apply.pathTo = function (url) {
        $location.path(url);
    };