Html Typescript枚举组合键值问题
我想在我的下拉列表中实现Html Typescript枚举组合键值问题,html,angular,typescript,Html,Angular,Typescript,我想在我的下拉列表中实现enum,但我在列表中同时获得键和值 我的枚举: export enum VMRole { "Kubemaster" = 0, "Kubeworker" = 1, "Other" = 2 } 我试图将enum分配给我的属性的我的类: export class VirtualMachine { role: VMRole; ... } 我的组成部分: export class AddVmComponent implements OnInit
enum
,但我在列表中同时获得键和值
我的枚举:
export enum VMRole {
"Kubemaster" = 0, "Kubeworker" = 1, "Other" = 2
}
我试图将enum
分配给我的属性的我的类:
export class VirtualMachine {
role: VMRole;
...
}
我的组成部分:
export class AddVmComponent implements OnInit {
model: any = {};
@ViewChild('addVMForm', { static: false }) formValues;
constructor(private alertify: AlertifyService, private vm: VmService, private route:
ActivatedRoute, private http: HttpClient) {
this.model.role = Object.keys(VMRole).filter(p => typeof p !== 'number')
}
}
我的HTML:
<select [ngModel]="model.role" class="form-control">
<option disabled>-Please choose role-</option>
<option *ngFor="let data of model.role | keyvalue">
{{data.value}}
</option>
</select>
-请选择角色-
{{data.value}}
截图:
当您尝试使用
p=>typeof p!='编号“
。刚刚测试了它返回的内容,其中表示的值与下面的数组类似-正如您所看到的,该数组只包含string
值:
因此,解决方案看起来很棘手,但它为您提供了解决方案:
Object.keys(VMRole).filter(p => !Number.isInteger(parseInt(p)))
或者更简单的解决方案,也适用于TypeScript:
Object.keys(VMRole).filter(p => isNaN(p as any))
请在此进一步阅读:
我希望这有帮助 第二种方法很有魅力。你救了我一天。谢谢