Hacker
Hacker

Reputation: 7906

anchorScroll not working, going to top of page

I have below code

$scope.scrollTo = function(id) {
     alog($location.hash());
     var toScrollId = "anchor" + id;
     if($location.hash() !== toScrollId){
       alog(" hash is not equal")
       $location.hash(toScrollId);
     }else{
      alog(" hash is equal")
       $anchorScroll();
     }
};

and html looks like

<button ng-click="scrollTo(raceid)">GO TO THIS</button>

<div ng-repeat="race in races" id="{{ 'anchor' + race.raceId}}">
</div>

But scroll always goes to top of page. Anything i am doing wrongly?

Upvotes: 1

Views: 1367

Answers (1)

Adam Kewley
Adam Kewley

Reputation: 1234

Your ng-repeat is assigning the id to the parent element (in this case, the races div) you might need to modify that first:

<div ng-repeat="race in races" id="races">
    <div id="{{ 'anchor' + race.raceId }}">Some race</div>
</div>

Upvotes: 1

Related Questions