2017-10-03 7 views
0

イオンアプリケーションの場合エラーが発生しました 'indexOf'のヌルのプロパティを読み取ることができません。ここでイオンの2 'indexOf'のヌルエラー

私はngClassを変更し、ストレージにカートを保存する項目をクリックにしたい

public cart: string[] = []; 


ionViewDidLoad(){ 
    this.cart = this.storage.get('cart').then((val) => { 
     return this.cart = val; 
    }); 
} 

を宣言上記の私のhtmlコードは

<ion-list> 
    <button ion-item *ngFor="let item of items" (click)="itemSelected(item)" [ngClass]="cart.indexOf(item)>=-1 ? 'active' : 'none'"> 
     <p >{{ item }}</p> 
     </button> 
</ion-list>  

とTSコード

itemSelected(item: string) { 
if (this.cart.indexOf(item) == -1) { 
    this.cart.push(item); 
    this.isActive = true; 
}else{ 
    this.cart.splice(this.cart.indexOf(item),1); 
    this.isActive = false; 
} 
this.storage.set('cart', cart);} 

です。

+0

試し '[ngClass] = "cart.includes(アイテム) 'アクティブ':? 'なし ''" – Rahul

+0

あなたは 'val'をログコンソールができますか? 'this.cart'への' this.storage.get() '呼び出しを設定する必要もありません。 –

+0

このエラーはいつ発生しますか?ページの読み込み時または要素をクリックした後 – porgo

答えて

0

なぜ2回割り当てるのですかthis.cart?割り当てる前にnullの値を確認してください。

以下のようにコードを修正してみてください。

ionViewDidLoad(){ 
    this.storage.get('cart').then((val) => { 
     if(val) 
     this.cart = val; 
    }); 
} 
関連する問題