Angularjs

Automatically Scroll To Top Of Page With UI-Router In AngularJS

Posted by Weston Ganger on July 15, 2015

I thought its a little stupid that ui-router doesnt scroll to the top of the page upon a state change by default. Here I will show you how to make it scroll to top.


myApp.config(function($stateProvider, $urlRouterProvider,$locationProvider,$uiViewScrollProvider){
  $uiViewScrollProvider.useAnchorScroll();

  ... other config ...
});


wrmc.run(function($rootScope,$location,$stateParams, $anchorScroll){
  $rootScope.$on('$stateChangeSuccess', function(event, toState){
    if($stateParams.scrollTo){
      $location.hash($stateParams.scrollTo);
      $anchorScroll();
    }
  });
});



Related External Links:


Posted in Angular and Tagged with ui-router angularjs scroll 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts