GreyScreenOfMeh
GreyScreenOfMeh

Reputation: 273

Can enums be used in AngularDart templates? How?

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

Answers (2)

GreyScreenOfMeh
GreyScreenOfMeh

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

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657496

From https://github.com/dart-lang/angular/blob/7f6858bc48c1d2a454a4bc350077d67c277c6516/angular/lib/src/core/metadata.dart#L206-L221

  /// 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

Related Questions