I have a <ion-scroll> that takes half my screen, since a client wanted a chat inside the app, I want to set the <ion-scroll> to always be at the bottom (always showing the last message) since it’s a chat module.
This is the relevant part of the code I was trying to use:
export class SquadPage {
@ViewChild('chatScroll') chatScroll: Content;
constructor(public nav: NavController, public squadData: SquadData, public profileData: ProfileData) {
/**
* I do all my initialization here and call the function after getting all the messages.
*/
this.scrollChatToBottom();
}
scrollChatToBottom(){
this.chatScroll.scrollToBottom(200);
}
sendMessage(message, squad, firstName){
this.squadData.sendMessage(message, squad, firstName).then( message => {
this.messageToSend = '';
this.scrollChatToBottom();
}, error => {
console.log(error);
});
}
}
Problem here is that I get a TypeError: this.chatScroll.scrollToBottom is not a function
You’re trying to call a component method before it has fully rendered. You need to wait and call it after the view is fully loaded and all the elements have been created.
That would work on the Content part, but I’m actually trying to use something like scrollToBottom() on an <ion-scroll>
Like I said in the original post, I’m trying to apply the logic from the Content docs there, is there a way to do that with an <ion-scroll>
For a bit of context: I have 2 <ion-scroll> inside an <ion-content> since my client needed 2 scrollable divs on the screen, one to show something like progress updates from users and the bottom one for a chat, since chat messages UI usually shows latest chat messages to the bottom I’d like to have that ion-scroll always scrolled to the bottom instead of it’s regular state of always scrolled to the top.
I tried, but the ion-scroll just didn’t have the methods, so I went with 2 pages instead of one for that app and used ScrollToBottom inside an ion-content
If you want I ended up finding a workaround for it, it’s not bad actually.
I added an ion-label to the bottom of my repeating list (i.e. chat messages for example)
<ion-label id="myLabel"> </ion-label>
And in the code where you need to call “ScrollToBottom()”:
ScrollToBottom(){
var element = document.getElementById("myLabel");
// I can't remember why I added a short timeout,
// but you might be able to use ngzone instead.
// the below works great though.
setTimeout(()=>{element.scrollIntoView(true)},200);
}
Thanks a lot @tonyawad88 ! works great !
All the information I found about it was using ion-content…
Seems weird for me that ion-scroll doesn’t have those basics methods …
I have an app to load some data from the remote source and implement an automatic scrolling.
When scrolling touches the bottom it has to start again from the top and again start to scroll down. This process has to continue.