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

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

提交回复
热议问题