AngularJS: Scroll to end of element after applying DOM changes

前端 未结 6 955
攒了一身酷
攒了一身酷 2020-12-29 20:16

I have a simple list of items. I want to be able to scroll to the bottom of the element displaying the items whenever I add more items. I understood there is no way of hooki

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 20:51

    A simple working example (no need for plugins or directives)...

    .controller('Controller', function($scope, $anchorScroll, $location, $timeout) {
    
      $scope.func = function(data) {
    
        // some data appending here...
    
        $timeout(function() {
          $location.hash('end');
          $anchorScroll();
        })
      }
    
    })
    

    The trick that did it for me was wrapping the anchorScroll command with $timeout, that way the scope was resolved and it automatically shifted to an element at the end of the page.

提交回复
热议问题