Reputation: 865
I am getting this error "Cannot read property 'scrollToBottom' of undefined" and haven't got a solution to this anywhere, hence this post:
Here is my use case: I have a custom accordion list, and on click of one of the list (since it will have some content) I want the user not to scroll to the bottom , hence using that property. I have only pasted the relevant code
import { Component, ViewChild } from '@angular/core';
import { NavController,Platform, Navbar, Content} from 'ionic-angular';
import { ToastController } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
@Component({
selector: 'page-profile',
templateUrl: 'profile.html'
})
export class ProfilePage {
@ViewChild('content') content: any;
constructor(public navCtrl: NavController, private nativeStorage : NativeStorage, private toastCtrl : ToastController, platform : Platform ) {
// other methods
}
ionViewDidLoad()
{
this.IonAccordion()
}
IonAccordion(){
this.accElement = document.getElementsByClassName('ion-accordion-header');
var i;
for (i = 0; i < this.accElement.length; i++) {
console.log(i)
this.accElement[i].addEventListener("click", function()
{
this.classList.toggle("active");
console.log("click");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
console.log(panel.style.maxHeight);
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
this.content.scrollToBottom(300);
}
});
}
}
}
<ion-content #content>
<ion-row>
<ion-col>
<div class="ion-accordion-header">Accordion 1 </div>
<div class="panel">
<p>Accordion content</p>
</div>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<div class="ion-accordion-header">Accordion 2 </div>
<div class="panel">
<p>Accordion content</p>
</div>
</ion-col>
</ion-row>
</ion-content>
cli packages: (F:\classette\node_modules)
@ionic/cli-plugin-cordova : 1.6.2 @ionic/cli-plugin-ionic-angular : 1.4.1 @ionic/cli-utils : 1.7.0 ionic (Ionic CLI) : 3.7.0
global packages:
Cordova CLI : 7.0.1
local packages:
@ionic/app-scripts : 1.3.7 Cordova Platforms : android 6.2.3 Ionic Framework : ionic-angular 3.3.0
System:
Node : v6.10.2 OS : Windows 10 npm : 3.10.10
Upvotes: 0
Views: 2998
Reputation: 109
Similar problem and my solution. Ionic 4 and Angular 8.
// header
import { ViewChild } from '@angular/core';
import { IonContent } from '@ionic/angular';
//elements
@ViewChild(IonContent, {static: true}) content: IonContent;
//functions
this.content.scrollToTop(500);
this.content.scrollToBottom(500);
Upvotes: 2
Reputation: 24234
This in your function no longer points to your class but it now refers to the DOM object inside your method IonAccordion. Use this trick instead:
import { Content } from 'ionic-angular';
@ViewChild(Content) content: Content;
IonAccordion(){
const scope = this;
this.accElement = document.getElementsByClassName('ion-accordion-header');
var i;
for (i = 0; i < this.accElement.length; i++) {
console.log(i)
this.accElement[i].addEventListener("click", function()
{
this.classList.toggle("active");
console.log("click");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
console.log(panel.style.maxHeight);
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
scope.content.scrollToBottom(300);
}
});
}
}
Upvotes: 1
Reputation: 6421
Besides the fat arrow function problem you've not imported the content from ionic-angular
, so you're not targeting your page content. Change your code to this:
import { Content } from 'ionic-angular';
@ViewChild(Content) content: Content;
After this your scrollToBottom
method should work.
Upvotes: 0