How to handle anchor hash linking in AngularJS

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

    I am not 100% sure if this works all the time, but in my application this gives me the expected behavior.

    Lets say you are on ABOUT page and you have the following route:

    yourApp.config(['$routeProvider', 
        function($routeProvider) {
            $routeProvider.
                when('/about', {
                    templateUrl: 'about.html',
                    controller: 'AboutCtrl'
                }).
                otherwise({
                    redirectTo: '/'
                });
            }
    ]);
    

    Now, in you HTML

    
    
    
    1
    2
    3

    In conclusion

    Including the page name before the anchor did the trick for me. Let me know about your thoughts.

    Downside

    This will re-render the page and then scroll to the anchor.

    UPDATE

    A better way is to add the following:

    First Part
    

提交回复
热议问题