I'm trying to do infinite scroll in ionic2 but whenever the page loads i get this error Cannot read property 'complete' of undefined
HTML
<ion-content>
<ion-list>
<ion-item *ngFor="let item of posts" (click)="passurl($event,item)">
<h2>{{item.title}}</h2>
</ion-item>
</ion-list>
<ion-infinite-scroll (ionInfinite)="ionViewDidLoad($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
JS
ionViewDidLoad(infiniteScroll){
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(data =>{
console.log(JSON.stringify(data));
setTimeout(() => {
for(var i=0;i<data.length;i++){
this.numofposts = data.length;
this.posts.push(data[i]);
}
infiniteScroll.complete();
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}
i followed the documentation on the ionic2 framework from their website. any help with this? thanks
You cannot use the ionViewDidLoad
hook as the target of the InfiniteScroll . You can put the same logic in a different method, like this:
<ion-infinite-scroll *ngIf="!hideInfinite" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
And in the component code:
public hideInfinite: boolean;
public loadData(infiniteScroll?: any): void {
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(
data => {
console.log(JSON.stringify(data));
if(!data || !data[0]) {
// Hide the infiniteScroll if there's no more data
this.hideInfinite = true;
}
setTimeout(() => {
for(var i=0;i<data.length;i++) {
this.numofposts = data.length;
this.posts.push(data[i]);
}
// Check if it's not null/undefined before calling the complete method
if(infiniteScroll) {
infiniteScroll.complete();
}
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}
If you want to use the same logic in the ionViewDidLoad
lifecycle hook, you could call the same method:
ionViewDidLoad(){
// You can now call the method without sending anything,
// since in the code we check if the infiniteScroll is defined
// before using its methods
this.loadData();
}
old ionic need to write like this
$event.state = "closed";
new version ionic need to write like this
infiniteScrollEvent.complete();
(or)
infiniteScrollEvent.target.complete();
ionViewDidLoad(infiniteScroll) {
this.http.get('http://mynewweb/templates/messages/titles.php?church_id=' + this.church_id + '&msg_type=' + this.NavParams.data.id + "&page=" + this.page).map(res => res.json()).subscribe(data => {
console.log(JSON.stringify(data));
if (infiniteScroll) {
infiniteScroll.complete();
}
});
this.page = this.page + 1;
}
public play=true;
public loadData(infiniteScroll?: any): void {
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(
data => {
this.numofposts=this.numofposts+data.length;
this.itemsdisplayed = this.itemsdisplayed + 10;
if(this.itemsdisplayed > this.numofposts) {
// Hide the infiniteScroll if there's no more data
this.play = false;
}
setTimeout(() => {
for(var i=0;i<data.length;i++) {
this.numofposts = data.length;
this.posts.push(data[i]);
}
// Check if it's not null/undefined before calling the complete method
if(infiniteScroll) {
infiniteScroll.complete();
}
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}
HTML
<ion-infinite-scroll *ngIf="play" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.