Scroll to bottom in chat box in angularjs

前端 未结 2 1843
Happy的楠姐
Happy的楠姐 2020-12-30 21:07

I am trying to automatically scroll to bottom whenever there is a new message.

My code moves the scrollbar but it does not take it to exact bottom. Kindly help. Her

2条回答
  •  一向
    一向 (楼主)
    2020-12-30 21:23

    Thanks @MajoB

    Here are my 2 cents including:

    • removed jQuery dependency
    • added a $timeout to make sure $digest cycle is finished

    ngScrollBottom.js:

    angular.module('myApp').directive('ngScrollBottom', ['$timeout', function ($timeout) {
      return {
        scope: {
          ngScrollBottom: "="
        },
        link: function ($scope, $element) {
          $scope.$watchCollection('ngScrollBottom', function (newValue) {
            if (newValue) {
              $timeout(function(){
                $element.scrollTop($element[0].scrollHeight);
              }, 0);
            }
          });
        }
      }
    }]);
    

提交回复
热议问题