Select based on enum in Angular2

前端 未结 11 2121
别那么骄傲
别那么骄傲 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条回答
  •  Happy的楠姐
    2020-11-28 23:44

    Another spin off of this answer, but this actually maps the values as numbers, instead of converting them to strings which is a bug. It also works with 0 based enums

    @Component({
      selector: 'my-app',
      providers: [],
      template: `
      `,
      directives: []
    })
    
    export class App {
      countries = CountryCodeEnum;
    
      constructor() {
        this.keys = Object.keys(this.countries)
                          .filter(f => !isNaN(Number(f)))
                          .map(k => parseInt(k));;
      }
    }
    

提交回复
热议问题