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 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts