I am currently developing a chat page with Ionic 4 and I'm attempting to implement an automatic scroll feature to the bottom of the page. However, the method I tried using doesn't seem to be working as expected:
import { IonContent } from "@ionic/angular";
export class ChatroomPage implements OnInit {
messageForm: FormGroup;
messages: any[];
messenger: any;
@ViewChild(IonContent) content: IonContent;
constructor(
private navExtras: NavExtrasService,
private api: RestApiService,
private httpNative: HTTP
) { }
ngOnInit() {
this.content.scrollToBottom(300);
}
}
In the HTML file:
<ion-header>
<ion-toolbar color="primary">
<ion-title>Chatroom</ion-title>
</ion-toolbar>
</ion-header>
<!-- display previous message -->
<ion-content padding id="content">
<ion-list>
<ion-item *ngFor="let message of messages">
{{ message.message }}
</ion-item>
</ion-list>
</ion-content>
<!-- chat message input -->
<ion-footer>
<form [formGroup]="messageForm" (submit)="sendMessage()" (keydown.enter)="sendMessage()">
<ion-input formControlName="message" type="text" placeholder="Enter your message"></ion-input>
<ion-button type="submit">Send</ion-button>
</form>
</ion-footer>
An error message is being shown:
ng:///ChatroomPageModule/ChatroomPage_Host.ngfactory.js:5 ERROR TypeError: Cannot read property 'scrollToBottom' of undefined
I would appreciate some guidance on what I may have done incorrectly. Most tutorials I've come across are for Ionic 3 which uses Content
from ionic-angular
rather than IonContent
from @ionic/angular
. It seems that the Content method in Ionic 4 does not have the scrollToBottom function available.