在 Angular 中使用 select 时如何仅显示产品 active=0

How to show only product active=0 when using select in Angular

我只想显示数据 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>

所有值在下拉列表 select 中显示正确,但我只想在下拉列表中显示具有 active:0

的 hb

拜托,你能问我如何只显示数据 active = 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>

为 ng-container 中的条件尝试 *ngIf

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

您可以将 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>