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

后端 未结 18 2039
北恋
北恋 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:23

    A little late to the party, but I've tried every possible method, and nothing worked correctly. Here is my elegant solution:

    I use a controller that governs all my pages with ui-router. It allows me to redirect users who aren't authenticated or validated to an appropriate location. Most people put their middleware in their app's config, but I required some http requests, therefore a global controller works better for me.

    My index.html looks like:

    My initCtrl.js looks like:

    angular.module('MyApp').controller('InitCtrl', function($rootScope, $timeout, $anchorScroll) {
        $rootScope.$on('$locationChangeStart', function(event, next, current){
            // middleware
        });
        $rootScope.$on("$locationChangeSuccess", function(){
            $timeout(function() {
                $anchorScroll('top-nav');
           });
        });
    });
    

    I've tried every possible option, this method works the best.

提交回复
热议问题