Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding enum into options in selector using ngFor

Tags:

angular

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

like image 818
Rasula Caldera Avatar asked Feb 21 '26 16:02

Rasula Caldera


1 Answers

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>
like image 174
Teddy Sterne Avatar answered Feb 24 '26 04:02

Teddy Sterne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!