html - Angular - 将表单值与数组值映射
问题描述
我有一个数组:
myArray = [ "Confirmed / Unconfirmed", "In / Out" ]
我有一个 HTML 选择
<select formControlName="choice_type">
<option></option>
<option *ngFor="let choice of myArray" [value]="choice"> {{ choice }} </option>
</select>
但是,我的表单使用数据库中的值进行了更新。在数据库中,选择的值为choice_confirmed_unconfirmed
或choice_in_out
。
是否有可能当我修补我的角度表单时,HTML 选择将显示数组中与数据库返回值匹配的值?
例如,如果表单值返回为choice_in_out
,则选择下拉菜单将被预选为In / Out
?
有没有办法将这些值的数据库格式与我希望它们在下拉列表中的显示方式绑定?
解决方案
您可以使用映射对象而不是数组,并将字符串映射到要显示的内容,同时保持与从 DB 获得的值相同。
myArray = {
choice_confirmed_unconfirmed: "Confirmed / Unconfirmed"
choice_in_out: "In / Out"
}
<option *ngFor="let choice of myArray | keyvalue" [value]="choice.key">
{{ choice.value }}
</option>
推荐阅读
- r - 什么是 R 中 scan() 函数的替代方法,它不仅适用于文件
- mysql - 使用 over() 和别名时的 mysql 错误
- powerbuilder - Appeon PowerBuilder2017 R2 配置
- android - Android添加了后按后无法显示的片段
- razor - 尝试搭建模型时出错
- office-js - Word 加载项无法安装在 Word 2016(MSO 16.0.4266.1001 64 位)上
- python - .如何获得列中出现次数最多的前 5 个名称
- r - 使用数据框列表中的名称重命名 Excel 选项卡名称
- core-data - 在核心数据获取请求中忽略谓词
- openlayers - 为什么我不能运行这个方法(openlayers)(ol.layer.Vector)