I want to bind an enum as options in a HTML selector
export enum MY_ENUM{
ONE = 'One',
TWO = 'Two',
THREE = 'Three'
}
How do I bind this as options for my HTML select using ngFor
I want to bind an enum as options in a HTML selector
export enum MY_ENUM{
ONE = 'One',
TWO = 'Two',
THREE = 'Three'
}
How do I bind this as options for my HTML select using ngFor
I would use Object.values
to get a list of the enum values like so:
this.options = Object.value(MY_ENUM);
And then in the template
<select>
<option *ngFor="let option of options" [value]="option">{{option}}</option>
</select>
You can do like this:
<select>
<option *ngFor="let opt of opts"
[value]="opt.id" >
{{opt.name}}
</option>
</select>
where opts is an array that you build from Enum
enum EnumExample {
OPT1 = "opt1",
OPT2 = "opt2"
}
export class Example {
public opts: any[];
constructor(){
this.opts = [
{id: "OPT1", name=EnumExample.OPT1},
{id: "OPT2", name=EnumExample.OPT2}];
}
}
I didnt find a way to do directly from Enum.