我正在尝试在 Ionic 2 中实现无限滚动功能,但没有调用该函数(应该在 onInfinite 上调用)。
在这里,我什至无法获得语句 console.log('Begin async operation'); 调用,这是 doInfinite 函数中的第一条语句,每当 Infinite Scroll 事件发生时都应该调用它。
搜索.html
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Search</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding style="overflow-y: auto">
<ion-searchbar (ionInput)="search($event)"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let product of products" (click)="itemTapped($event, product)">
{{ product.name }}
</ion-item>
</ion-list>
<ion-infinite-scroll (ionInfinite)="doInfinite($event)">
<ion-infinite-scroll-content
loadingSpinner="bubbles"
loadingText="Loading more data..."></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
搜索.ts
import { Component } from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import { FetchProducts } from '../../providers/fetch-products.service';
import { SearchProduct } from '../../providers/search-product.service';
import { NavController, NavParams} from 'ionic-angular';
import { ProductDetailPage} from '../product-detail/product-detail';
@Component({
selector: 'page-search',
templateUrl: 'search.html',
providers: [ FetchProducts , SearchProduct ]
})
export class SearchPage {
public products: Array<any> = [];
public s_products: any;
public searchQuery: any = '';
items: string[];
public productDetail: any;
public perpage: number = 2;
public start: number =1;
public offset: any = 0;
public canLoadMore : any = true;
constructor(public http: Http, public fetchProducts: FetchProducts, public searchProduct : SearchProduct, public navCtrl: NavController, public navParams: NavParams) {
this.loadProducts();
}
itemTapped(event, product) {
this.navCtrl.push(ProductDetailPage, {
product: product
});
console.log("itemTapped:" + product);
}
search(searchEvent) {
this.searchQuery = searchEvent.target.value;
console.log("Typed value is:" + this.searchQuery + "Offset Value is dsd:" + this.offset);
if (this.searchQuery.trim() !== '' || this.searchQuery.trim().length > 3) {
this.fetchProducts.searchProducts(this.searchQuery, 0).subscribe(products => {
this.products=products;
console.log(this.products);
});
}
this.canLoadMore = true;
this.offset += 2;
}
doInfinite(infiniteScroll: any) {
console.log('Begin async operation');
setTimeout(() => {
console.log("Typed value is:" + this.searchQuery + "Offset Value is:" + this.offset);
this.fetchProducts.searchProducts(this.searchQuery, this.offset).subscribe(products => {
this.products.push(products);
console.log(this.products);
if(products.length < 10){
this.canLoadMore = false;
console.log("No more products");
return;
}
else{
this.offset += 2;
}
});
console.log('Async operation has ended');
infiniteScroll.complete();
}, 500);
}
loadProducts(){
this.fetchProducts.load()
.then(data => {;
for(var i = 0; i < ( data.length ); i++){
this.products.push(data[i].name);
}
console.log(this.products);
});
}
}