uksz uksz - 6 months ago 192
AngularJS Question

TypeScript + AngularJS 1: How to connect enum with select directive?

I am having an issue with joining enum with SELECT from HTML. My enum structure looks as follows:

enum Color {
Green,
Red,
Blue
}


I would like to somehow bind this enum to the html select tag, using angular. Any ideas?

Thanks

uksz

Answer

When your enum is compiled to JavaScript, it will become the following object. You can see the equivalent JavaScript here at TypeScript playground.

{
   0: "Green"
   1: "Red"
   2: "Blue"
   "Blue": 2
   "Green": 0
   "Red": 1   
}

Now as you can see, if you were to bind this with a <select> you will get both the numeric and non-numeric items. Assuming you only want non numeric items to show up in the list, you will need to filter it.