Changing route doesn't scroll to top in the new page

后端 未结 18 1971
北恋
北恋 2020-11-29 15:11

I've found some undesired, at least for me, behaviour when the route changes. In the step 11 of the tutorial http://angular.github.io/angular-phonecat/step-11/app/#/phon

相关标签:
18条回答
  • 2020-11-29 15:28

    I found this solution. If you go to a new view the function gets executed.

    var app = angular.module('hoofdModule', ['ngRoute']);
    
        app.controller('indexController', function ($scope, $window) {
            $scope.$on('$viewContentLoaded', function () {
                $window.scrollTo(0, 0);
            });
        });
    
    0 讨论(0)
  • 2020-11-29 15:28

    Setting autoScroll to true did not the trick for me, so I did choose another solution. I built a service that hooks in every time the route changes and that uses the built-in $anchorScroll service to scroll to top. Works for me :-).

    Service:

     (function() {
        "use strict";
    
        angular
            .module("mymodule")
            .factory("pageSwitch", pageSwitch);
    
        pageSwitch.$inject = ["$rootScope", "$anchorScroll"];
    
        function pageSwitch($rootScope, $anchorScroll) {
            var registerListener = _.once(function() {
                $rootScope.$on("$locationChangeSuccess", scrollToTop);
            });
    
            return {
                registerListener: registerListener
            };
    
            function scrollToTop() {
                $anchorScroll();
            }
        }
    }());
    

    Registration:

    angular.module("mymodule").run(["pageSwitch", function (pageSwitch) {
        pageSwitch.registerListener();
    }]);
    
    0 讨论(0)
  • 2020-11-29 15:34

    After an hour or two of trying every combination of ui-view autoscroll=true, $stateChangeStart, $locationChangeStart, $uiViewScrollProvider.useAnchorScroll(), $provide('$uiViewScroll', ...), and many others, I couldn't get scroll-to-top-on-new-page to work as expected.

    This was ultimately what worked for me. It captures pushState and replaceState and only updates scroll position when new pages are navigated to (back/forward button retain their scroll positions):

    .run(function($anchorScroll, $window) {
      // hack to scroll to top when navigating to new URLS but not back/forward
      var wrap = function(method) {
        var orig = $window.window.history[method];
        $window.window.history[method] = function() {
          var retval = orig.apply(this, Array.prototype.slice.call(arguments));
          $anchorScroll();
          return retval;
        };
      };
      wrap('pushState');
      wrap('replaceState');
    })
    
    0 讨论(0)
  • 2020-11-29 15:34

    you can use this javascript

    $anchorScroll()
    
    0 讨论(0)
  • 2020-11-29 15:34

    This Worked for me including autoscroll

    <div class="ngView" autoscroll="true" >

    0 讨论(0)
  • 2020-11-29 15:35

    This code worked great for me .. I hope it will also work great for you .. All you have to do is just inject $anchorScroll to your run block and apply listener function to the rootScope like I have done in the below example ..

     angular.module('myAngularApp')
    .run(function($rootScope, Auth, $state, $anchorScroll){
        $rootScope.$on("$locationChangeSuccess", function(){
            $anchorScroll();
        });
    

    Here's the calling order of Angularjs Module:

    1. app.config()
    2. app.run()
    3. directive's compile functions (if they are found in the dom)
    4. app.controller()
    5. directive's link functions (again, if found)

    RUN BLOCK get executed after the injector is created and are used to kickstart the application.. it means when you redirected to the new route view ,the listener in the run block calls the

    $anchorScroll()

    and you can now see the scroll starts to the top with the new routed view :)

    0 讨论(0)
提交回复
热议问题