1

I'm trying to use $anchorScroll to scroll to an id if you click a paragraph. For some reason it only works if you click the paragraph twice.

View:

<div class="col-xs-12">
<p ng-repeat="item in items" ng-click="gotoAnchor(item .visibleName)">    {{item .visibleName}}</p>
</div>

<div class="col-xs-12" ng-repeat="item in items" >
<div id="anchor{{item .visibleName}}">
    <h1>{{item.idPrimary}}</h1>
</div>

Controller:

$scope.gotoAnchor  = function(x) {
    var newHash = 'anchor' + x;
    if ($location.hash() != newHash) {
      $location.hash('anchor' + x);
    } else {
      $anchorScroll();
    }
  };
  • That's because you only `$anchorScroll` when the `newHash` matches the current one... Why bother with the `if`? – Tony Nov 02 '15 at 16:18
  • Yes but even if i don' use the if i have to click twice. $scope.gotoAnchor = function(x) { var newHash = 'anchor' + x; $location.hash('anchor' + x); $anchorScroll(); }; – lekrem unam Nov 02 '15 at 16:28
  • Since you seem to be following the [documentation](https://docs.angularjs.org/api/ng/service/$anchorScroll) are you sure your anchors are right? Ids must be unique, do any of your items have duplicate `visibleName`s? – Tony Nov 02 '15 at 17:05
  • Perhaps this question solves your problem: http://stackoverflow.com/questions/29637614/anchorscroll-only-works-after-second-click – Tony Nov 02 '15 at 17:07
  • Yes I am sure that there are not any duplicates. – lekrem unam Nov 02 '15 at 17:55

0 Answers0