F
F
fantom00052015-12-12 15:23:19
Angular
fantom0005, 2015-12-12 15:23:19

How to make anchor links with AngularJS?

How to make anchor links work with Angular? the essence of this is a page with code:

<a href="#info">info</a>
<section id="info">
Info....
</section>

Usually everything works, but if you connect the angular, then instead of index#info, index/#/info appears in the address bar and, accordingly, no transition occurs.
How to do this without crutches and hardcode, because I think the creators of angular should have taken care of the navigation inside the page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-12-12
@fantom0005

Everything is not very simple here.
There is such a service: https://docs.angularjs.org/api/ng/service/$anchorScroll
With it, you can do something like this:

// some controller
['$scope', $anchorScroll, function($scope, $anchorScroll) {
    $scope.scrollTo = function(id) {
        $location.hash(id);
        $anchorScroll();
    }
}];

If hashes are used everywhere only in the case of anchors, you can hang processing on the onLocationChangeSuccess event.
$rootScope.$on('$routeChangeSuccess', function(newRoute, oldRoute) {
    if($location.hash()) $anchorScroll();  
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question