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

Posted By Weston Ganger

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:

Article Topic:Software Development - Javascript

Date:July 15, 2015