Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're looking for <a href="https://docs.angularjs.org/api/ng/service/$anchorScroll" rel="nofollow noreferrer"><code>$anchorScroll()</code></a>.</p> <p><a href="https://docs.angularjs.org/api/ng/service/$anchorScroll" rel="nofollow noreferrer">Here's the (crappy) documentation.</a></p> <p><a href="https://github.com/angular/angular.js/blob/master/src/ng/anchorScroll.js" rel="nofollow noreferrer">And here's the source.</a></p> <p>Basically you just inject it and call it in your controller, and it will scroll you to any element with the id found in <code>$location.hash()</code></p> <pre><code>app.controller('TestCtrl', function($scope, $location, $anchorScroll) { $scope.scrollTo = function(id) { $location.hash(id); $anchorScroll(); } }); &lt;a ng-click="scrollTo('foo')"&gt;Foo&lt;/a&gt; &lt;div id="foo"&gt;Here you are&lt;/div&gt; </code></pre> <p><a href="http://plnkr.co/edit/PCO051UJS8EHbdkmFV40?p=preview" rel="nofollow noreferrer">Here is a plunker to demonstrate</a></p> <p><strong>EDIT: to use this with routing</strong></p> <p>Set up your angular routing as usual, then just add the following code.</p> <pre><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(); }); }); </code></pre> <p>and your link would look like this:</p> <pre><code>&lt;a href="#/test?scrollTo=foo"&gt;Test/Foo&lt;/a&gt; </code></pre> <p>Here is a <a href="http://plnkr.co/edit/De6bBrkHpojgAbEvHszu" rel="nofollow noreferrer">Plunker demonstrating scrolling with routing and $anchorScroll</a></p> <p><strong>And even simpler:</strong></p> <pre><code>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(); }); }); </code></pre> <p>and your link would look like this:</p> <pre><code>&lt;a href="#/test#foo"&gt;Test/Foo&lt;/a&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload