How to handle anchor hash linking in AngularJS

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

    You're looking for $anchorScroll().

    Here's the (crappy) documentation.

    And here's the source.

    Basically you just inject it and call it in your controller, and it will scroll you to any element with the id found in $location.hash()

    app.controller('TestCtrl', function($scope, $location, $anchorScroll) {
       $scope.scrollTo = function(id) {
          $location.hash(id);
          $anchorScroll();
       }
    });
    
    <a ng-click="scrollTo('foo')">Foo</a>
    
    <div id="foo">Here you are</div>
    

    Here is a plunker to demonstrate

    EDIT: to use this with routing

    Set up your angular routing as usual, then just add the following code.

    app.run(function($rootScope, $location, $anchorScroll, $routeParams) {
      //when the route is changed scroll to the proper element.
      $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
        $location.hash($routeParams.scrollTo);
        $anchorScroll();  
      });
    });
    

    and your link would look like this:

    <a href="#/test?scrollTo=foo">Test/Foo</a>
    

    Here is a Plunker demonstrating scrolling with routing and $anchorScroll

    And even simpler:

    app.run(function($rootScope, $location, $anchorScroll) {
      //when the route is changed scroll to the proper element.
      $rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
        if($location.hash()) $anchorScroll();  
      });
    });
    

    and your link would look like this:

    <a href="#/test#foo">Test/Foo</a>
    
    0 讨论(0)
  • 2020-11-22 13:21

    This may be a new attribute for ngView, but I've been able to get it anchor hash links to work with angular-route using the ngView autoscroll attribute and 'double-hashes'.

    ngView (see autoscroll)

    (The following code was used with angular-strap)

    <!-- use the autoscroll attribute to scroll to hash on $viewContentLoaded -->    
    <div ng-view="" autoscroll></div>
    
    <!-- A.href link for bs-scrollspy from angular-strap -->
    <!-- A.ngHref for autoscroll on current route without a location change -->
    <ul class="nav bs-sidenav">
      <li data-target="#main-html5"><a href="#main-html5" ng-href="##main-html5">HTML5</a></li>
      <li data-target="#main-angular"><a href="#main-angular" ng-href="##main-angular" >Angular</a></li>
      <li data-target="#main-karma"><a href="#main-karma" ng-href="##main-karma">Karma</a></li>
    </ul>
    
    0 讨论(0)
  • 2020-11-22 13:21

    See https://code.angularjs.org/1.4.10/docs/api/ngRoute/provider/$routeProvider

    [reloadOnSearch=true] - {boolean=} - reload route when only $location.search() or $location.hash() changes.

    Setting this to false did the trick without all of the above for me.

    0 讨论(0)
  • 2020-11-22 13:21

    Sometime in angularjs application hash navigation not work and bootstrap jquery javascript libraries make extensive use of this type of navigation, to make it work add target="_self" to anchor tag. e.g. <a data-toggle="tab" href="#id_of_div_to_navigate" target="_self">

    0 讨论(0)
  • 2020-11-22 13:22

    In my case, I noticed that the routing logic was kicking in if I modified the $location.hash(). The following trick worked..

    $scope.scrollTo = function(id) {
        var old = $location.hash();
        $location.hash(id);
        $anchorScroll();
        //reset to old to keep any additional routing logic from kicking in
        $location.hash(old);
    };
    
    0 讨论(0)
  • 2020-11-22 13:23

    If you always know the route, you can simply append the anchor like this:

    href="#/route#anchorID
    

    where route is the current angular route and anchorID matches an <a id="anchorID"> somewhere on the page

    0 讨论(0)
提交回复
热议问题