2017-03-02 87 views
1

我有一個包含消息的消息容器塊。我怎樣才能移動滾動到這個div的底部?我有一個指令將其移動到底部,但它不能正常工作。在div元素的結尾處移動滾動條angularjs chat

模板:

<div class="message-area scroll default always-visible" scroll-bottom="$ctrl.allMessages"> 
     <div class="container-column-fluid"> 
      <message-container ng-repeat="message in $ctrl.allMessages" message="message"></message-container> 
     </div> 
    </div> 

指令:

function scrollBottom() { 
    return { 
     scope: { 
      scrollBottom: "<" 
     }, 
     restrict: 'A', 
     link: function(scope, element, attr) { 
      scope.$watchCollection('scrollBottom', function(newVal) { 
       if (newVal) { 
        $timeout(function() { 
         element[0].scrollTop = element[0].scrollHeight; 
        }, 0); 
       } 

      }); 
     } 
    }; 
} 

export { scrollBottom }; 

回答

1

哦,我忘了插入$超時的功能......所有的一天的工作......

function scrollBottom($timeout) { 
    return { 
     scope: { 
      scrollBottom: "<" 
     }, 
     restrict: 'A', 
     link: function(scope, element, attr) { 
      scope.$watchCollection('scrollBottom', function(newVal) { 
       if (newVal) { 
        $timeout(function() { 
         element[0].scrollTop = element[0].scrollHeight; 
        }, 0); 
       } 

      }); 
     } 
    }; 
} 

export { scrollBottom }; 
+0

默認值$ timeout的第二個參數(延遲)是0.所以你可以省略。 –