Angular ui-router scroll to top, not to ui-view

后端 未结 10 1368
情话喂你
情话喂你 2020-12-12 16:00

I\'ve just upgraded to ui-router 0.2.8 from 0.2.0 and I\'ve noticed that when the state changes, the scroll position jumps to the top of te child <

10条回答
  •  天涯浪人
    2020-12-12 16:34

    If you wanted to do this with views conditionally, you could place this in the controller view like so:

    .state('exampleState', {
        url: '/exampleURL',
        controller: 'ExampleCtrl as examplectrl',
        onEnter: function($rootScope) {
                $rootScope.$on('$viewContentLoaded',function(){
                jQuery('html, body').animate({ scrollTop: 0 }, 200);
            });
        }
    }).
    

    or optionally which might keep your state.js file cleaner place the above in the controller for given view:

    function ExampleCtrl ($scope, $rootScope) {
    
        $rootScope.$on('$viewContentLoaded',function(){
                jQuery('html, body').animate({ scrollTop: 0 }, 200);
            });
    }
    

    Hope this helps someone, please let me know if I'm missing something. I used the latter and works great for me.

提交回复
热议问题