Select based on enum in Angular2

前端 未结 11 2142
别那么骄傲
别那么骄傲 2020-11-28 23:26

I have this enum (I\'m using TypeScript) :

export enum CountryCodeEnum {
    France = 1,
    Belgium = 2
}

I would like to build a

11条回答
  •  孤独总比滥情好
    2020-11-28 23:53

    I've preferred to have a simple utility function shared across my Angular App, to convert the enum into a standard array to build selects:

    export function enumSelector(definition) {
      return Object.keys(definition)
        .map(key => ({ value: definition[key], title: key }));
    }
    

    to fill a variable in the Component with:

    public countries = enumSelector(CountryCodeEnum);
    

    and then fill my Material Select as my old array based ones:

    
      
        {{ c.title }}
      
    
    

    Thanks for this thread!

提交回复
热议问题