Use of Enums in Angular 8 HTML template for *ngIf

16,136

Solution 1

in the TS

import { SomeEnum } from 'path-to-file';

public get SomeEnum() {
  return SomeEnum; 
}

in the HTML use

*ngIf="SomeEnum.someValue === 'abc'"

EDIT: Time goes by and we learn more as a developer, the approach I'm using right now doesn't use the get method. Both solutions work, just choose the one you like the most.

in the TS

import { SomeEnum } from 'path-to-file';

export class ClassName {
  readonly SomeEnum = SomeEnum;
}

in the HTML use

*ngIf="SomeEnum.someValue === 'abc'"

Solution 2

You'll have to declare it as a property first:

import { Component } from '@angular/core';
import { SomeEnum } from './global';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = SomeEnum.someValue;
  importedSomeEnum = SomeEnum;
}

And then use it in the template:

<span *ngIf="name === importedSomeEnum.someValue">This has some value</span>

Here's a Working Demo for your ref.

Solution 3

You can declare a field equal to the SomeEnum enum (it may be imported from another file) as a public class field in the component file. Then it will be accessible in the template.

// component 
export class AppComponent  {
  name = SomeEnum.someValue;
  enum = SomeEnum;
}

// template
<span *ngIf="name === enum.someValue">This has some value</value>

Solution 4

Yes, the template cannot refer to the enum directly. There are few ways to do this. 1. Add Enum reference to the component ts file like below

someEnum = SomeEnum;

then you will be able to use the reference in your template like this

<span *ngIf="name === someEnum.someValue">This has some value</value>
  1. The second way is to call a function from your template with name as parameter and compare it in the typescript file

<span *ngIf="checkCondition(name)">This has some value</value>

Share:
16,136
Ling Vu
Author by

Ling Vu

Frontend Developer Passioned about web development. Mainly working on the frontend professionaly with Angular and React, but in my free time I broaden my knowledge on mobile and backend development as well. Lots of experience in different professional projects, starting from big enterprise software, to small app extensions. Code quality is quite important for me and I would always do the trade off - development time vs quality, because in the long run it will always pay off. Working Examples: Created a small raspi demo for sensor hardware communicating through bluetooth with a fullstack application. In this case the raspi was the web server. Developed enterprise software using cutting edge tech, always the newest version of Angular, NgRx, scss. Always try to avoid code smell, have highest test code coverage and full documentation. For smaller applications I always like to use React. Always with Typescript, Redux and scss. I created with React smaller exentions. I am not looking for a job, but if you have something interesting, just tell me.

Updated on June 06, 2022

Comments

  • Ling Vu
    Ling Vu almost 2 years

    How can I use Enums in the Angular 8 template?

    component.ts

    import { Component } from '@angular/core';
    import { SomeEnum } from './global';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = SomeEnum.someValue;
    }
    
    

    component.html

    <span *ngIf="name === SomeEnum.someValue">This has some value</value>
    

    This currently doesn't work, since the template has no reference to SomeEnum. How can I solve it?

    ERROR Error: Cannot read property 'someValue' of undefined