简体   繁体   English

使用ngif ionic4过滤ngfor中的json数据

[英]Filtering json data in ngfor with ngif ionic4

i'm create ionic apps with angular 8, i try to display datas from json file and filter it with ngif in home.page.html, here's my code我正在创建角度为 8 的离子应用程序,我尝试显示来自 json 文件的数据并在 home.page.html 中使用 ngif 对其进行过滤,这是我的代码

<ion-slides class="links_slide" [options]="sliderConfig">
<ion-slide *ngFor="let item of items">
  <div *ngIf="item.status === 'marketplace'">
    <ion-card class="list-container" (click)='webview()' [style.backgroundImage]="'url('+item.iconlink+')'">
    </ion-card>
    <ion-card-title class="label">{{ item.name }}</ion-card-title>
  </div>
</ion-slide>

and here's the json这是 json

[
    {
        "id": "0",
        "status":"marketplace",
        "name": "Webstore",
        "link": "https://youthscarf.com",
        "iconlink": "http://atmoweb.id/wp-content/uploads/2019/10/ysweb.jpg"
    }
    {
        "id": "6",
        "status":"oa",
        "name": "Instagram Signature",
        "link": "https://instagram.com/youthcatalog_id/",
        "iconlink": "http://atmoweb.id/wp-content/uploads/2019/10/listdummy.jpg"
    }
]

but now i have problem, list of data that i have filtered, they don't display but they still showing up in html, i mean like this.但现在我有问题,我过滤的数据列表,它们不显示但它们仍然以 html 格式显示,我的意思是这样。

here's the image这是图片

i mark the data that showing up in html they don't display.我将显示在 html 中的数据标记为不显示。 how to solve this如何解决这个问题

The issue is that you're looping on the <ion-slide> so regardless of whether or not the *ngIf is evaluated to true, the <ion-slide> will display render with empty content.问题是您在<ion-slide>上循环,因此无论*ngIf是否被评估为 true, <ion-slide>都将显示内容为空的渲染。 You can use the *ngFor on an <ng-container> to loop through the items.您可以在<ng-container>上使用*ngFor来循环遍历项目。 Then add the *ngIf to the <ion-slide> .然后将*ngIf添加到<ion-slide>

<ng-container *ngFor="let item of items">
  <ion-slide *ngIf="item.status === 'marketplace'">
    <div >
      ...
    </div>
  </ion-slide>
</ng-container>

https://alligator.io/angular/ng-container-element/ https://alligator.io/angular/ng-container-element/


Another option is to filter the items by calling a method in you ts file.另一种选择是通过调用 ts 文件中的方法来过滤项目。

items-list.component.html项目列表.component.html

<ion-slide *ngFor="let item of getMarketplaceItems(items)">
  <div >
    ...
  </div>
</ion-slide>

items-list.component.ts项目列表.component.ts

getMarketplaceItems(items) {
  return items.filter(item => item.status === 'marketplace');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM