I am writing my own chat interface, to help me better understand angularJs and socket.io.
My chat window is fixed, with overflow set to auto. I am using the following directive to manipulate the DOM to scroll, so that as each message is pushed onto the model array, it will be visible at the bottom of the window.
app.directive('plnkScroll', [function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
angular.element(element)[0].scrollIntoView();
}
};
}]);
In theory, this works. However, if the message is long enough to take up more than one line, it does not scroll so that the entire line is clearly visible.
This is because the scrollIntoView
method is being executed on the DOM while the angular expression has yet to be evaluated (in this case, {{item.text}}
)
Is it possible to execute this function after the Angular expression has been evaluated and the model data has been injected into the DOM?
here is my plunker, to help illustrate what I am doing.
p.s. using jQuery is not an option for me, I would like to do things "the angular way".