You are looking for $anchorScroll()
Basically, you need to set up routes as usual by passing a scroll parameter, for example:
url: '/first/:scrollTo',
and just add the following by typing $anchorScroll and it will scroll you to any item with the id found in $location.hash()
app.run(function($rootScope, $location, $anchorScroll, $stateParams, $timeout) { $rootScope.$on('$stateChangeSuccess', function(newRoute, oldRoute) { $timeout(function() { $location.hash($stateParams.scrollTo); $anchorScroll() }, 100) }); })
Then in your html the links should look like this:
<a href ui-sref="first({scrollTo: 'foo'})">First / Foo</a>
Alternatively, you can create the onEnter: function in your state to do this:
.state('first', { url: '/first/:scrollTo', controller: 'FirstCtrl', templateUrl: 'first.html', onEnter: function ($location, $stateParams, $anchorScroll, $timeout) { $timeout(function() { $location.hash($stateParams.scrollTo); $anchorScroll() }, 100) } })
Keeping things in a state:
.state('first', { url: '/first/:scrollTo', controller: 'FirstCtrl', templateUrl: 'first.html', onEnter: scrollTo })
var scrollTo = function() { function ($location, $stateParams, $anchorScroll, $timeout) { $timeout(function() { $location.hash($stateParams.scrollTo); $anchorScroll() }, 100) } };