How to handle anchor hash linking in AngularJS

后端 未结 27 911
后悔当初
后悔当初 2020-11-22 12:22

Do any of you know how to nicely handle anchor hash linking in AngularJS?

I have the following markup for a simple FAQ-page



        
27条回答
  •  醉酒成梦
    2020-11-22 13:01

    You could try to use anchorScroll.

    Example

    So the controller would be:

    app.controller('MainCtrl', function($scope, $location, $anchorScroll, $routeParams) {
      $scope.scrollTo = function(id) {
         $location.hash(id);
         $anchorScroll();
      }
    });
    

    And the view:

    Scroll to #foo
    

    ...and no secret for the anchor id:

    This is #foo

提交回复
热议问题