How can I use ngFor to iterate over Typescript Enum as an array of strings

后端 未结 11 1537
借酒劲吻你
借酒劲吻你 2020-12-13 12:03

I am using Angular2 and Typscript. I have an enum:

export enum Role {
    ServiceAdmin, CompanyAdmin, Foreman, AgentForeman, 
    CrewMember, AgentCrewMembe         


        
11条回答
  •  时光取名叫无心
    2020-12-13 12:18

    I have the enum:

    export enum FileCategory {
      passport = 'Multipass',
      agreement = 'Personal agreement',
      contract = 'Contract',
      photo = 'Self photos',
      other = 'Other'
    }
    

    In the component ts file:

    export class MyBestComponent implements OnInit {
      fileCategory = FileCategory;
    
      // returns keys of enum
      fileKeys(): Array {
        const keys = Object.keys(this.fileCategory);
        return keys;
      }
    
      // returns values of enum
      fileVals(): Array {
        const keys = Object.keys(this.fileCategory);
        return keys.map(el => Object(this.fileCategory)[el]);
      }
    

    In the HTML template display these enum's values and keys:

      {{cat}}
      {{cat}}
    

提交回复
热议问题