Angularjs浏览器后退按钮可阻止部分加载并导致页面刷新

时间:2013-10-21 13:52:31

标签: angularjs

我有一个应用程序,当我按照页面上的链接时表现正常,但如果我使用浏览器的后退和前进按钮,它会以下列方式中断:

  1. 它向服务器发送请求,但仅在“返回”。
  2. 模板根本不会呈现。
  3. 此外,当我点击“返回”从页面B转到页面A时,chrome的“刷新/停止”按钮快速地在顶部选项之间闪烁,并且重复尝试返回和前进会导致更长时间的闪烁。

    以下是我认为相关的代码段:

    编辑:我正在研究一个plnkr,但该网站目前无法运行。我会在它启动时更新,我可以验证不良行为

    编辑2:这是plnkr,但它有问题。它找不到app.js路由中指定的templateUrls,不知道为什么。无论如何,这是代码http://plnkr.co/edit/6cQtnvLi10sJKW8jVzVM

    编辑3:在朋友的帮助下,我认为问题来自于在轨道上使用turbo-links 4.我现在无法测试它,但是如果可以的话,我可以发布答案

    file:app.js

    window.App = angular.module('app', [
        'templates',
        'ui.bootstrap'
    ])
    .config(['$routeProvider', '$locationProvider', 
        function($routeProvider, $locationProvider) {
            $locationProvider.html5Mode(true);
    
            $routeProvider
                .when('/', {
                    controller: 'HomeCtrl',
                    templateUrl: 'home.html'
                })
                .when('/bars', {
                        controller: 'BarsPublicCtrl',
                        templateUrl: 'bars_public.html'
                })
                .when('/bars/:bar_name', {
                    controller: 'BarsDetailPublicCtrl',
                    templateUrl: 'bars_detail_public.html'
                })
                .otherwise({redirectTo: '/'});
    }]);
    

    file:bars_public_ctrl.js

     App.controller('BarsPublicCtrl', ['$scope', '$location', 'BarsPublicDataFactory',
            function($scope, $location, BarsPublicDataFactory) {
                    // memoization
                    $scope.bars = $scope.bars || BarsPublicDataFactory.getBars();
          }
        ]);
    

    BarsPublicDataFactory只返回虚假数据的静态数组,与以下代码段中的工厂相同

    file:bars_detail_public_ctrl.js

    App.controller('BarsDetailPublicCtrl', ['$scope', '$routeParams', 'BarsDetailPublicDataFactory',
        function($scope, $routeParams, BarsDetailPublicDataFactory) {
          $scope.bar = {};
          $scope.bar.name = $routeParams.barId;
          $scope.barDetails = BarsDetailPublicDataFactory.getBaz($routeParams.bar_name);
          $scope.Bazs = BarsDetailPublicDataFactory.getBazs();
    }]);
    

    file:bars_public.html

    <div class="container">
        <div ng-repeat="bar in bars">
            <div class="row">
                <div class="col-sm-12">
                    <a href="/bars/{{bar.name}}">
                        <h4 style="display:inline;">{{ bar.name }}</h4>
                    </a>
                </div>
            </div>
            <hr />
        </div>
    </div>
    

    file:bars_detail_public.html

    <select ng-model="searchSelect.style" style="width:100%;">
        <option value='' selected>All</option>
        <option ng-repeat="baz in bazs">{{baz}}</option>
    </select>
    <div>
        <accordion close-others="true">
            <accordion-group ng-repeat="foo in foos | filter:searchSelect">
                <accordion-heading>
                    <div>
                        <h3>{{foo.name}}</h3>                
                        <em>{{foo.style}}</em>
                    </div>
                </accordion-heading>
            </accordion-group>
        </accordion>
    </div>
    

    如果您还有其他需要,请告诉我。

2 个答案:

答案 0 :(得分:4)

事实证明这个问题是由Rails 4的turbolink引起的。我没有提到它,因为我没有意识到它很重要。

我不知道究竟是什么导致了它,但是turbolinks会注入一些javascript,而且据我所知,它会劫持一些导致页面重新加载的事件,当你使用浏览按钮时会破坏我的应用程序。< / p>

所以我遵循了这个建议:http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4 它工作得很好!希望其他人可以受益。

答案 1 :(得分:0)

befoure: [安装] jquery_turbo_links [安装] turbolinks

改变: [安装] jquery_turbo_links [除去] turbolinks

有效!