Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dartlang: Using enums in Angular2 templates

can someone tell me if and how it is possible to use Dart enums in angular2 view templates?

It's suggested in Typescript to copy the enum into a component-scope. This works because an enum in javascript is a simple instance variable with a prototype.

But since enums in dart are first class citizens, they cannot be moved to an instance variable.

Thanks in advance!

like image 267
Benjamin Jesuiter Avatar asked Feb 11 '17 21:02

Benjamin Jesuiter


2 Answers

We're working on bringing formal support, but you can always add a getter for now:

enum MyEnum {
  stateA,
  stateB,
}

class MyComponent {
  MyEnum get stateA => MyEnum.stateA;
}
like image 162
matanlurey Avatar answered Nov 03 '22 19:11

matanlurey


Use the exports property of your Component class. Copied from here in the AngularDart Guide.

enum MyEnum { foo, bar, baz }

@Component(
  selector: 'example',
  exports: const [MyEnum],
  template: '<p>{{MyEnum.bar}}</p>',
)
class Example {}
like image 4
Luke Hanks Avatar answered Nov 03 '22 19:11

Luke Hanks