如果我有一个枚举,例如:
@Component(...)
class MyComponent {
MyEnum myEnum;
...
}
如何在模板中使用此枚举?例如。
<div *ngIf="myEnum == MyEnum.SOME_OPTION">
...
</div>
编辑:我特别感兴趣的是如何使用枚举进行比较。
/// A list of identifiers that may be referenced in the template. /// /// ## Small Example /// /// Suppose you want to use an enum value in your template: /// /// enum MyEnum { foo, bar, baz } /// /// @Component( /// selector: 'example', /// exports: const [MyEnum], /// template: '<p>{{MyEnum.bar}}</p>', /// ) /// class Example {} /// final List<Object> exports;
另见https://github.com/dart-lang/angular/blob/master/angular/CHANGELOG.md#new-features-6
我找到了一个方法。我认为它有点hackish,但它的工作原理。
给枚举一个返回字符串的getter。通过用单引号括起来将模板中的枚举转换为字符串。现在比较两个字符串。
String get choice => myEnum.toString();
...
choice == 'MyEnum.SOME_OPTION'