How to handle anchor hash linking in AngularJS

后端 未结 27 1139
后悔当初
后悔当初 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:24

    Here is kind of dirty workaround by creating custom directive that will scrolls to specified element (with hardcoded "faq")

    app.directive('h3', function($routeParams) {
      return {
        restrict: 'E',
        link: function(scope, element, attrs){        
            if ('faq'+$routeParams.v == attrs.id) {
              setTimeout(function() {
                 window.scrollTo(0, element[0].offsetTop);
              },1);        
            }
        }
      };
    });
    

    http://plnkr.co/edit/Po37JFeP5IsNoz5ZycFs?p=preview

提交回复
热议问题