首页 > 解决方案 > 我如何将 id 值作为下拉列表中的数字传递

问题描述

我的 html 包含以下代码

  <select name ="InCharge" formControlName="InCharge"  class="form-control">
                                <option value="" disabled>Select</option>
                                <option  value="{{name.id}}" *ngFor="let name of gdinchargemstr ;let i=index">{{name.name}}</option>
                            </select>

gdinchargemstr 是一个包含来自 api 的值的对象

我的ts文件如下

this.employeeForm= this.fb.group({
Description: ['',Validators.required],
InCharge: [''] ,
ContactNumber: [null],
Address:[''],
Rackdetails: this.fb.array([
this.addRackFormGroup()
])
});

标签: angular

解决方案


使用 [ngValue] 而不是 [value]

  <select name ="InCharge" formControlName="InCharge" class="form-control">
      <option [value]=null disabled>Select</option>
        <option  [ngValue]="name.id" *ngFor="let name of gdinchargemstr;let i=index"> 
         {{name.name}}</option>
     </select>

推荐阅读