How to handle anchor hash linking in AngularJS

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

    If you don't like to use ng-click here's an alternate solution. It uses a filter to generate the correct url based on the current state. My example uses ui.router.

    The benefit is that the user will see where the link goes on hover.

    My element
    

    The filter:

    .filter('anchor', ['$state', function($state) {
        return function(id) {
            return '/#' + $state.current.url + '#' + id;
        };
    }])
    

提交回复
热议问题