1

I'm trying to implement a scrollTo effect in AngularJS on an ng-click event, following the example in this Plunkr.

I'm trying to scroll to categoryhead, like so:

myApp.controller('FoodCtrl', function ($scope, $http, $location, $anchorScroll) {

//other stuff 

$scope.loadFood = function(category) {
 //other stuff

  //Scroll category head to top of page
  $scope.categoryhead = "#" + category;
  $location.hash($scope.categoryhead);
  $anchorScroll();

 //Ajax load stuff
 };  
});

I have the value of $scope.categoryhead printing out in the template and can see that it is the correct ID for each element, but the window doesn't get scrolled. Am I doing something wrong in trying to pass the element ID to $anchorscroll()?

Here's my Plunkr

2 Answers 2

1

Figured it out after a while. I was specifying $scope.categoryhead incorrectly.

Here's a Plunkr with anchorscroll(); to various page elements.

Sign up to request clarification or add additional context in comments.

Comments

-1

you missed out the css to apply so as no height is given its not showing scroll

.scroll{
  height: 350px;
  overflow: auto;
}

here is the updated plunker

1 Comment

No, the original doesn't depend on a height & overflow setting on .scroll. It still works if you remove them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.