Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Typescript枚举组合键值问题_Html_Angular_Typescript - Fatal编程技术网

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))
请在此进一步阅读:


  • 我希望这有帮助

    第二种方法很有魅力。你救了我一天。谢谢