Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically scroll to the bottom in AngularJS?

Tags:

angularjs

In my AngularJS app there is a controller which contains a list with messages:

$scope.messages = []

This list is updated as the application runs. In the template it is rendered in the following way:

<div style="height: 200px; overflow: auto">
...
    <li ng-repeat="msg in messages">{{msg}}</li>
...

But when more messages comes than fits the 200px height, the scroll remains on the top.

How to automatically scroll to the bottom of the message list?

like image 669
ticcky Avatar asked Jul 02 '14 09:07

ticcky


People also ask

How to scroll down in AngularJs?

AngularJs Scroll to Bottom : There are many ways to scroll to the bottom of the page. You can use $anchorScroll and $location directives to scroll to the bottom of the page.

How do you implement auto scroll?

The main task is implemented in JavaScript. We have initialized 2 functions ( startScroll and stopScroll ). The startScroll() function will enable the scrolling every time Enter is pressed. And the stopScroll() will get enabled when the Enter press is detected.


1 Answers

There is a directive "scroll-glue" from this library https://github.com/Luegg/angularjs-scroll-glue that solves the problem.

Just add scroll-glue directive to the div:

<div style="height: 200px; overflow: auto" scroll-glue>
...
like image 118
ticcky Avatar answered Oct 26 '22 09:10

ticcky