How to handle anchor hash linking in AngularJS

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

    My solution with ng-route was this simple directive:

       app.directive('scrollto',
           function ($anchorScroll,$location) {
                return {
                    link: function (scope, element, attrs) {
                        element.click(function (e) {
                            e.preventDefault();
                            $location.hash(attrs["scrollto"]);
                            $anchorScroll();
                        });
                    }
                };
        })
    

    The html is looking like:

    link
    

提交回复
热议问题