Live example: https://angular-txvcna.stackblitz.io/ (Code).
I have a "chat" component and I want to get the div to scroll to the bottom each time a message is added.
I'm adding a new message by pushing an item to an array (this.messages.push(message)
), and trying to scroll to the bottom right after that. It seems like the scrolling operation this.chatboxBody.nativeElement.scrollTop = this.chatboxBody.nativeElement.scrollHeight;
is before the lifecycle hooks of angular, because scrollHeight
has the older value of the height as it excutes.
At the moment I use setTimeout
to fix it, but it really feels like a shortcut. How can I fix that? (Ideally - to somehow subscribe to the hook that updates the child component)