Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing const enums in Angular html template

Assume I have a const enum:

export const enum MyConstEnum{
    Value1 = 'Value1',
    Value2 = 'Value2',
    Value3 = 'Value3'
}

Now I want to use it in my Angular template:

<span *ngIf="name === MyConstEnum.Value1">This has some value</value>

However, this is not possible, because MyConstEnum is not seen by template. So the question is how to access const enum in Angular html template?

If enum won't be const like this

export enum MyEnum{
    Value1 = 'Value1',
    Value2 = 'Value2',
    Value3 = 'Value3'
}

there is a solution to create property in templates' component

  public get MyEnumInComponent() {
    return MyEnum;
  }

and MyEnumInComponent will be accessible in HTML. However, I have const enum.

For this I cannot defined property like above. What is the solution (except changing const enum to enum)?

like image 333
renathy Avatar asked Aug 10 '20 08:08

renathy


1 Answers

I can see on this link https://github.com/angular/angular/issues/25963 that it is known issue and it is specifically with const enum.

enter image description here

There is also a work arround suggested in the discussion on the url:
templateImports: [someConstant, UserStatus, isDevMode]
This would not work, but the below could:

templateImports: {someConstant, UserStatus, isDevMode}
like image 153
Apurva Pathak Avatar answered Oct 23 '22 04:10

Apurva Pathak