How to hide unused items with ngFor ionic?

梦想与她 提交于 2019-12-02 06:12:48

问题


im trying to hide unused items from ngFor, the problem is, i did hide it successfully but its place still exists and empty like the image below:

My ts file:

coupon: any;
  couponz() {
    var data=[];
    for (let co of this.shared.couponz){
    data.push({ code: co.code, coEmail: co.email_restrictions[0], expiry: co.date_expires });
    this.coupon = data;
    console.log(this.coupon)
    }return data
  }

my data provider:

@Injectable()
export class SharedDataProvider {

  public couponz;

this.config.Woocommerce.getAsync("coupons/").then((data) => {
  this.couponz = JSON.parse(data.body);
});

my html:

  <ion-list padding>
    <ion-item *ngFor="let c of coupon; trackBy: trackElement"> 
      <div *ngIf="c.coEmail == shared.customerData.email">
      {{c.expiry}}
      {{c.code}}
      </div>
      </ion-item>
  </ion-list>

回答1:


Two options:

Refactor the template into the following:

<ion-list padding>
  <ng-container *ngFor="let c of coupon; trackBy: trackElement">
    <ion-item *ngIf="c.coEmail == shared.customerData.email">
      <div >
        {{c.expiry}}
        {{c.code}}
      </div>
     </ion-item>
  </ng-container>
 </ion-list>

Or store the filtered elements of the list in another instance member of the component:

this.filteredCoupons = this.coupons.filter(c => c.coEmail == this.shared.customerData.email)

<ion-list padding>
    <ion-item *ngFor="let c of filteredCoupons; trackBy: trackElement"> 
      <div>
      {{c.expiry}}
      {{c.code}}
      </div>
     </ion-item>
  </ion-list>


来源:https://stackoverflow.com/questions/55091192/how-to-hide-unused-items-with-ngfor-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!