【问题标题】:Angularjs browser back button stops partials from loading and causes page refreshesAngularjs浏览器后退按钮停止加载部分并导致页面刷新
【发布时间】:2015-11-18 21:34:43
【问题描述】:

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

  1. 它向服务器发送请求,但仅在“返回”时。
  2. 根本不渲染模板。

此外,当我点击“返回”从页面 B 转到页面 A 时,chrome 的“刷新/停止”按钮在顶部选项之间快速闪烁,反复尝试返回和前进会导致闪烁时间更长。

以下是我认为相关的代码sn-ps:

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

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

编辑 3:在朋友的帮助下,我认为问题出在 Rails 4 上使用 turbo-links。我现在无法测试它,但如果可以,我会发布答案.

文件: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: '/'});
}]);

文件:bars_public_ctrl.js

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

BarsPublicDataFactory 只是返回一个假数据的静态数组,与下面 sn-p 中的工厂相同

文件: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();
}]);

文件: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>

文件: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>

如果你还需要什么,请告诉我。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    原来问题是由 Rails 4 的 turbolinks 引起的。我没有提到它,因为我没有意识到它很重要。

    我不知道到底是什么原因造成的,但是 turbolinks 注入了一些 javascript,据我所知,它劫持了一些事件,当您使用破坏了我的应用程序的浏览按钮时,会导致页面重新加载。

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

    【讨论】:

    • 非常感谢!这是我们的 Angular 应用程序中最奇怪的错误,我们花了很多时间调试它......你的答案来了。
    【解决方案2】:

    之前: [已安装]jquery_turbo_links [已安装]turbolinks

    改变: [已安装]jquery_turbo_links [删除]涡轮链接

    成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多