Overview
In this article, we will discuss Anchor Scroll Service in AngularJS. $anchorscroll Service is used to jump to a specified element in a page. Let's see with an example.
For more articles on Angular JS, refer to
- Overview Of AngularJS
- Modules And Controller In AngularJS
- Controllers in AngularJS
- AngularJS ng src Directive
- AngularJS ng-Repeat Directive
- Handling Events In AngularJS
- How To Use Two-Way Data Binding In AngularJS
- Filters In AngularJS
- Sorting Data In AngularJS
- Sorting Rows By Table Header In AngularJS
- Creating Custom Filters In AngularJS
- ng-Hide And ng-Show In AngularJS
- ng-init Directive In AngularJS
- Search And MultiSearch In AngularJS
- ngInclude Directive In AngularJS
- $http Service In AngularJS
- Consuming ASP.NET WebService In AngularJS
In the overview section, $anchorscroll Service is used to jump to a specified element in a page .
In this example, I had written some data on Angular JS and had two buttons. Go to bottom and go to top. My HTML page will look like:

As you can see from the screenshot, shown above, I added some of the Angular JS content from Wiki. Now, I had taken two buttons; bottom and top. When you click bottom, it should go to bottom and when you scroll down, it should go to top, as well. Thus, our page will look like:

The Page should go to bottom and,

Here, the page should go to top.
Now, in HTML tag, add ng-app directive and write the demo. In our model, we will write a line as:
- <html xmlns="http://www.w3.org/1999/xhtml" ng-app="demo">
- var demo=angular.module("demo",[]);
- var demo = angular.module("demo", [])
- .controller("demoController", function () {
- });
- var demo = angular.module("demo", []).controller("demoController", function($scope, $location, $anchorScroll)
- {
- $scope.scrollTo = function() {}
- });
- $scope.scrollTo = function (scrollLocation)
Now, the scrolllocation i.e $location has an hash function. When the respective ID's are clicked, # symbol will be appended as:

Thus, our function is:
- $location.hash(scrollLocation);
- $anchorScroll();
- var demo = angular.module("demo", []).controller("demoController", function($scope, $location, $anchorScroll)
- {
- $scope.scrollTo = function(scrollLocation)
- {
- $location.hash(scrollLocation);
- $anchorScroll();
- }
- });
- <body ng-controller="demoController">
- <button id="Top" ng-click="scrollTo('bottom')">Bottom</button>
- <button id="bottom" ng-click="scrollTo('Top')">Top</button>

Now, look at the URL at the top.

It's because of the hash function, when you click top button, the scroll will move upwards and note the URL and the ID has changed to TOP

If you want to add space to the top of the button, we will add in our model as : $anchorScroll.yOffset=20;
Thus, when you reload the page, you will get the space between the page and button.
Thus $anchorservice is used to jump to the specific element of a page.
- $location service hash method appends the hash fragments to the URL .
- $anchorscroll() method reads the hash fragments in URL and jumps to the specific elements.
- Yofset is used to add the space to the page.
Conclusion: Thus, it was about AnchorScroll in AngularJS. Hope, this article was helpful.

Srinivas RajolePosted Aug 13, 2016, 8:30 AM
Good one...
Vignesh ManiPosted Aug 11, 2016, 11:36 AM
Nice
Bikesh SrivastavaPosted Aug 11, 2016, 2:37 AM
Very nice