Angular2在html值属性中使用枚举值 [英] Angular2 use enum value in html value attribute

查看:608
本文介绍了Angular2在html值属性中使用枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个枚举值来设置HTML属性的选定值:

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>

但是我得到无法读取未定义的属性'FirstValue'"

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

是否可以使用枚举值作为html属性的值?

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

推荐答案

您的模板只能访问其组件类的成员变量.因此,如果要使用枚举的值,请将其(枚举)分配给成员变量.

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.

在您的组件中,

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

然后,您可以自由访问模板中枚举的元素.

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

这篇关于Angular2在html值属性中使用枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆