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

后端 未结 18 2052
北恋
北恋 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

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

提交回复
热议问题