2

If i want to leverage enums which exist outside the scope of my component.

export enum Template { Foo = 0, Bar = 1, Fuzz = 2 }

but when trying to leverage it my template file, it will throw an error.

<div *ngIf="model.type == Template.Foo"></div>

for example. When serving the application, it does not understand what Template is. If I create a property in my component, Template, as follows it will work:

Template = Template

essentially creating the enum inside the scope of the component as its own property. I just dont like it though, it feels dirty and not right.

I thought there was a property in the Component decorator to essentially add exports for the template, but that does not seem to be the case.

Is the way I am doing it correct, or is there a way which doesnt feel like a hack?

Thank you.

Edit The reason I asked this, is that the AngularDart way of doing it is through an exports property in the Component Annotation. See: https://webdev.dartlang.org/api/angular/angular/Component/exports and was not sure if this is just a divergence of technology or if there was an option that does similar without property assignment.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • 3
    I think that assigning the enum to a property of the component class is correct. As I mentioned in [this answer](https://stackoverflow.com/a/50357404/1009922), the [expresson context](https://angular.io/guide/template-syntax#expression-context) of the template code is the component instance. – ConnorsFan Feb 19 '19 at 21:57
  • Agreed. If you think about the component as the "template resolution context", then everything that the template binds to should be encapsulated in that template. So if there is a need to bind to an external enum or service, define a property for it in the component to use for binding. – DeborahK Feb 19 '19 at 22:01
  • I added an edit which explains my reasoning. I was doing angular dart for the better part of 2 years, and they leveraged a property in the Component annotation, so you can see why I was confused. – Fallenreaper Feb 19 '19 at 22:38

0 Answers0