如何在Ionic 3中使用带有虚拟滚动的无限滚动?

时间:2017-11-13 07:22:52

标签: html5 angular typescript ionic-framework ionic3

新的离子和角度,因此使用无限滚动和虚拟滚动会有一些麻烦。无数据即。图像,文本,点击功能是在打印无限滚动时从打字稿中提取的。仅显示空的离子卡。

以下代码:

HTML:

<ion-header>
  <ion-navbar color="dark">
    <ion-title><button>Appname</button></ion-title>
  </ion-navbar>
</ion-header>

<ion-content class="card-background-page">
  <ion-list [virtualScroll]="categories">
    <ion-item *virtualItem="let category" class="itemcss">
        <ion-card (click)="category.golink()" class="wallcard">
          <img class='homecardimage' [src]="category.Url" />  
            <div class="card-title">{{category.Name}}</div>
          </ion-card>
    </ion-item>
  </ion-list>

  <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
      <ion-infinite-scroll-content></ion-infinite-scroll-content>
    </ion-infinite-scroll>

</ion-content>

打字稿:

    import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams,AlertController,LoadingController,ActionSheetController } from 'ionic-angular';
    import { Network } from '@ionic-native/network';
    import {HomePage} from '../home/home';

    @IonicPage()
    @Component({
      selector: 'page-category',
      templateUrl: 'category.html',
    })

      categories = [];
      categories_arr= [
        { Name: 'name1' ,Url:'Cloudinary Link1',golink:()=>this.function1()},
        { Name: 'name2',Url:'Cloudinary Link2',golink:()=>this.function2()},
        { Name: 'name3',Url:'Cloudinary Link3',golink:()=>this.function3() },
        { Name: 'name4',Url:'Cloudinary Link4',golink:()=>this.function4()},
        { Name: 'name5',Url:'Cloudinary Link5',golink:()=>this.function5()}
      ];

    constructor(public navCtrl: NavController, public navParams: NavParams,private network: Network,public alertCtrl: AlertController,public loadingCtrl: LoadingController,public actionSheetCtrl: ActionSheetController) 
      {
        for (let category = 0; category < 3; category++) {
          this.categories.push( this.categories.length +'.'+ this.categories_arr[this.categories.length]);
        }
      }

    doInfinite(infiniteScroll) {
        console.log('Begin async operation');

        setTimeout(() => {
          for (let category = 0; category < 3; category++) {
            this.categories.push( this.categories.length +'.'+ this.categories_arr[this.categories.length]);
          }

          console.log('Async operation has ended');
          infiniteScroll.complete();
        }, 500);
      }

function1(){
    this.navCtrl.push('Page1');
   }   

function2(){
    this.navCtrl.push('Page2');
   }   

function3(){
    this.navCtrl.push('Page3');
   }   

function4(){
    this.navCtrl.push('Page4');
   }   

function5(){
    this.navCtrl.push('Page5');
   }   

Screenshots

当我尝试此代码时,没有显示数据(名称,网址图片,点击功能),仅显示空的离子卡。为什么?纠正我的代码。 感谢。

1 个答案:

答案 0 :(得分:0)

我想我找到了我的问题的答案。可能或者可能不适合你的情况。不管怎样, 我对打字稿代码进行了更改。其他一切都是一样的。与问题没什么大不同。

打字稿:

    import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams,AlertController,LoadingController,ActionSheetController } from 'ionic-angular';
    import { Network } from '@ionic-native/network';
    import {HomePage} from '../home/home';

    @IonicPage()
    @Component({
      selector: 'page-category',
      templateUrl: 'category.html',
    })

      categories = [];
      categories_arr= [
        { Name: 'name1' ,Url:'Cloudinary Link1',golink:()=>this.function1()},
        { Name: 'name2',Url:'Cloudinary Link2',golink:()=>this.function2()},
        { Name: 'name3',Url:'Cloudinary Link3',golink:()=>this.function3() },
        { Name: 'name4',Url:'Cloudinary Link4',golink:()=>this.function4()},
        { Name: 'name5',Url:'Cloudinary Link5',golink:()=>this.function5()}
      ];

    constructor(public navCtrl: NavController, public navParams: NavParams,private network: Network,public alertCtrl: AlertController,public loadingCtrl: LoadingController,public actionSheetCtrl: ActionSheetController) 
      {
        for (let category = 0; category < 3; category++) {
  this.categories.push( this.categories_arr[this.categories.length]);
  //Changed in this line.
        }
      }

    doInfinite(infiniteScroll) {
        console.log('Begin async operation');

        setTimeout(() => {
          for (let category = 0; category < 3; category++) {
      if(this.categories_arr[this.categories.length]!=null){//added IF
      this.categories.push( this.categories_arr[this.categories.length]);     
      //Changed in this line.              
         }
            }

          console.log('Async operation has ended');
          infiniteScroll.complete();
        }, 500);
      }

function1(){
    this.navCtrl.push('Page1');
   }   

function2(){
    this.navCtrl.push('Page2');
   }   

function3(){
    this.navCtrl.push('Page3');
   }   

function4(){
    this.navCtrl.push('Page4');
   }   

function5(){
    this.navCtrl.push('Page5');
   }