Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 use enum value in html value attribute

Tags:

I am trying to use an enum value to set the selected value of an HTML attribute:

export enum MyEnum {     FirstValue,     SecondValue } export function MyEnumAware(constructor: Function) {     constructor.prototype.MyEnum = MyEnum; }  @MyEnumAware @Component({     templateUrl: "./lot-edit.component.html",     styleUrls: ["./lot-edit.component.css"], }) export class LotEditComponent implements OnInit {     @Input() myEnumValue: MyEnum ; 

}

<td><input name="status" type="radio" [value]="MyEnum.FirstValue" [(ngModel)]="myEnumValue"></td> <td><input name="status" type="radio" [value]="MyEnum.SecondValue" [(ngModel)]="myEnumValue"></td> 

however I get "Cannot read property 'FirstValue' of undefined"

Is there a way to use an enum value as the value of html attributes?

like image 323
wonbyte Avatar asked Feb 26 '17 03:02

wonbyte


1 Answers

Your template can only access member variables of its component class. So, if you want to use the enum's values, assign it (the Enum) to a member variable.

In your component,

export class MyComp {   MyEnum = MyEnum;   ..... }    

Then you're free to access the elements of the enum in your template.

like image 153
snorkpete Avatar answered Nov 09 '22 21:11

snorkpete