Reputation: 273
If I have an enum, e.g:
@Component(...)
class MyComponent {
MyEnum myEnum;
...
}
How can I use this enum in a template? E.g.
<div *ngIf="myEnum == MyEnum.SOME_OPTION">
...
</div>
Edit: I'm specifically interested in how to use enums in comparison.
Upvotes: 5
Views: 387
Reputation: 273
I found a way. I suppose it's a bit hackish, but it works.
Give the enum a getter that returns a string. Convert the enum in the template to a string by surrounding it with single quotes. Now compare the two strings.
String get choice => myEnum.toString();
...
choice == 'MyEnum.SOME_OPTION'
Upvotes: 0
Reputation: 657496
/// 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;
See also https://github.com/dart-lang/angular/blob/master/angular/CHANGELOG.md#new-features-6
Upvotes: 3