简体   繁体   English

Angular | NgRx 检查 object 中的 null 字段,然后将其放入下拉列表

[英]Angular | NgRx Check for null field in an object before putting it in a list of dropdown

I have a html dropdown which is sourcing data from an api, the api returns an array of objects [in the component].我有一个 html 下拉列表,它从 api 获取数据,api 返回一个对象数组[在组件中]。 The Dropdown uses a field value which is in most objects, but not all. Dropdown 使用大多数对象中的字段值,但不是全部。

So it causes, some fields entries to show blank in the dropdown.因此,某些字段条目在下拉列表中显示为空白。

<div class="col-sm-5">
                    <label>DropDown<label class="field-importance" style="padding-left: 14rem">Required</label></label>
                    <select formControlName="Entry" placeholder="Select Entry" required>
                        <option value="null" disabled="true" [selected]="true" [hidden]="true">Select Entry</option>
                        <option *ngFor="let e of entry" [value]="e.val">{{e.val}}</option>
                    </select>
</div>

在此处输入图像描述

Thank you soo much非常感谢

You could do a *ngIf check for val property before using it.您可以在使用之前对val属性进行*ngIf检查。 Try the following尝试以下

<div class="col-sm-5">
  <label>DropDown<label class="field-importance" style="padding-left: 14rem">Required</label></label>
  <select formControlName="Entry" placeholder="Select Entry" required>
    <option value="null" disabled="true" [selected]="true" [hidden]="true">Select Entry</option>
    <ng-container *ngFor="let e of entry">
      <ng-container *ngIf="e.val">
        <option [value]="e.val">{{e.val}}</option>
      </ng-container>
    </ng-container>
  </select>
</div>

I would recommend filtering out null values from your array in the ts code.我建议从 ts 代码中的数组中过滤掉 null 值。 So when you are initializing data to the entry variable, do something like this:因此,当您将数据初始化到入口变量时,请执行以下操作:

this.entry = data.filter(x => x.val);

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

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