1

我有以下代码

$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();
     }
};

和 html 看起来像

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

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

但滚动总是到页面顶部。我做错了什么?

4

1 回答 1

1

ng-repeat正在将 分配id给父元素(在本例中为racesdiv),您可能需要先对其进行修改:

<div ng-repeat="race in races" id="races">
    <div id="{{ 'anchor' + race.raceId }}">Some race</div>
</div>
于 2015-08-25T07:43:54.393 回答