angular - 我如何将 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()
])
});
解决方案
使用 [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>
推荐阅读
- c# - System.ComponentModel.DataAnnotations 属性无法识别。无法使用 StringLength 属性验证空字符串
- ios - React 本机性能模拟器与真实设备
- php - 已弃用:非静态方法 Category::objdb() 不应在第 246 行的 /home4/muqit117/kharredo.com/admin/models/Category.php 中静态调用
- powershell - 用于收集主机名的 PowerShell Ping 脚本
- python - 用 C++ 实现 CNN 深度学习模型
- java - 更新数据库中的用户权限 Spring boot OAuth 2.0
- c++ - 将 const 引用传递给线程
- reactjs - React 中饼图的自定义标签
- javascript - 如何通过javascript设置随机backgroundImage
- javascript - WebRTC - 如何调整视频流的麦克风音量?