在Angular中使用select时,如何仅显示product active = 0

问题描述 投票:0回答:3

我想只显示数据active = 0。我在JSON中获取所有数据,如下所示:

{
    "StatusCode": 0,
    "StatusMessage": "OK",
    "StatusDescription": [
        {   "h_id": "1",
            "active": 0,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "4324fdfd",
            "client_id": null
        },
        {   "h_id": "2",
            "active": 0,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "3435fdf",
            "client_id": null
        },
        {   "h_id": "3",
            "active": 0,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "fgdge43",
            "client_id": null 
        },
        {   "h_id": "4",
            "active": 1,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "edf43",
            "client_id": 1
        },
        {
            "h_id": "5",
            "active": 1,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "3456677777",
            "client_id": 2
        },
        ....
        {
           "h_id": "10",
            "active": 1,
            "date_created": "2018-08-02T15:28:52.000Z",
            "serial_number": "JDLk32",
            "client_id": 200
        }
      ]
     }

在html中,我编写此代码以显示下拉列表中的所有数据:

<div class="input-field col s12">
  <select formControlName="h_id" id="h_id" materialize="material_select" [materializeSelectOptions]="hbp" >
    <option value="" disabled selected>Select Hb</option>
    <option *ngFor="let hb of hbp" [value]="hb.h_id">{{hb.serial_number}}</option>
  </select>
</div>

所有值在下拉选择中显示正确,但我想在下拉列表中仅显示具有active:0的hb

拜托,你能问我怎么只显示数据active = 0

angular typescript materialize
3个回答
0
投票

在组件中创建一个getter。

get zeroHbp() {
  return this.hbp && this.hbp.filter(hb => hb.active === 0) || [];
}
<option *ngFor="let hb of zeroHbp" [value]="hb.h_id">{{hb.serial_number}}</option>

0
投票

尝试使用ng-container中的条件* ngIf

**<select>
   <ng-container *ngFor="let hb of hbp">
     <option *ngIf="hb.active == 0"  [ngValue]="hb.h_id" ></option>
   </ng-container>
</select>**

0
投票

您可以将html代码更改为以下内容:

<select id="h_id">
  <option value="" disabled selected>Select Hb</option>
  <ng-container *ngFor="let hb of hbp.StatusDescription">
    <option *ngIf="hb.active === 0">{{hb.serial_number}}</option>
  </ng-container>
</select>
© www.soinside.com 2019 - 2024. All rights reserved.