HiveBrain v1.2.0
Get Started
← Back to all entries
debughtmlangularCritical

Cannot approach Typescript enum within HTML

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptenumapproachhtmlcannotwithin

Problem

I made an enum with Typescript to use in MyService.service.ts MyComponent.component.ts and MyComponent.component.html.

export enum ConnectionResult {
    Success,
    Failed     
}


I can easily get and compare a defined enum variable from MyService.service.ts:

this.result = this.myService.getConnectionResult();

switch(this.result)  
{
    case ConnectionResult.Failed:
         doSomething();
         break;
    case ConnectionResult.Success:
         doSomething();
         break;
}


I also wanted to use the enum for a comparison within my HTML using the *ngIf statement:


            

       


The code compiles but the browser gives me an error:


Cannot read property of undefined

With the following html indication error line:

Does anyone know why the enum cannot be approached like this?

Solution

You will have to write it in the following way in .ts file.

enum Tenure { day, week, all }

export class AppComponent {
    tenure = Tenure.day
    TenureType = Tenure
}


And now in html you can use this like

*ngIf = "tenure == TenureType.day ? selectedStyle : unSelectedStyle"


I hope it is more clear now. :)

Code Snippets

enum Tenure { day, week, all }

export class AppComponent {
    tenure = Tenure.day
    TenureType = Tenure
}
*ngIf = "tenure == TenureType.day ? selectedStyle : unSelectedStyle"

Context

Stack Overflow Q#44045311, score: 101

Revisions (0)

No revisions yet.