2017-10-19 63 views
1

Dears,Ionic VirtualScrool无法读取属性“长度”空

我试图使用离子3的虚拟滚动,但它不工作。

我有我提供这样的功能:

getActiveAds(){ 
    return this.afDb.list<AngularFireList<any>>('/ads-active', ref => ref.orderByChild('adPlanPriority').startAt(1).endAt(3)) 
    } 

在我的列表页,我有这个:

constructor(public navCtrl: NavController, public navParams: NavParams, public loadingCtrl: LoadingController, public adProvider: AdProvider) { 
    this.loading = this.loadingCtrl.create(); 
    this.loading.present(); 

    this.ads = this.adProvider.getActiveAds().valueChanges() 
    this.ads.subscribe((cat)=> { 
     this.loading.dismiss() 
    }) 
    } 

和我list.html这样的:

<ion-list no-lines [virtualScroll]="ads | async"> 
     <button ion-item *virtualItem="let ad" (click)="onAdSelect(ad)" class="aero-item "> 
      <ion-thumbnail item-start> 
       <img src="assets/images/noimage.jpg" /> 
      </ion-thumbnail> 
      <h2>{{ ad.model}}</h2> 

     </button> 
    </ion-list> 

随着此代码,我收到此错误:

Cannot read property 'length' of null 
TypeError: Cannot read property 'length' of null 
    at VirtualScroll._stepDOMWrite (http://localhost:8100/build/vendor.js:92118:60) 
    at http://localhost:8100/build/vendor.js:92078:23 
    at dispatch (http://localhost:8100/build/vendor.js:20601:9) 
    at DomController._flush (http://localhost:8100/build/vendor.js:20545:13) 
    at rafCallback (http://localhost:8100/build/vendor.js:20534:22) 

我在做什么错?

+0

我不确定virtualScroll可以使用异步。尝试用ngFor替换并查看结果 – Duannx

回答

0

我得到了同样的问题,并找到了解决方法,虽然它不是最佳的。

您从the documentation[virtualScroll]预计阵列。而你的this.ads返回Observable。这就是为什么你得到Cannot read property 'length' of null

解决方法:先订阅它,然后在视图中显示(没有异步管道),并且在完成时记住unsubsribe。

ads: any[] = []; 
constructor(public adProvider: AdProvider) { 
    this.adProvider.getActiveAds().valueChanges().subscribe(results => this.ads = results); 
    } 
+0

我照你说的做,但是现在,我得到一个不同的错误:“不能读取属性'处理'null –

+0

也许你可以在这个[link]上看看我的这个测试项目, (https://github.com/vuhieptran95/myFirstIonicApp)。请注意**演讲者页面**,我在Observable上应用virtualscroll。希望这可以帮助:) –

相关问题