Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an enum in an Angular Component

My Angular Components tend to have a global state (or "mode") so I am looking for a way to code this efficiently. What I tried to do is this:

@Component({       .... }) export class AbcComponent implements OnInit {    enum State {     init, view, edit, create, wait   }   state: State = State.init; 

The idea is that the functions inside AbcComponent can drive the template's operation simply by setting the state property. For example:

<div class="col" *ngIf="state === State.view"> ... </div> 

The problem is that the enum definition cannot appear inside the class structure. And then if I move it outside the class structure then the template doesn't have it within its local scope.

Is there a different way to do this?

P.S. If it is of any interest what I have been doing is I have several boolean properties, one for each state. For example modeInit modeView etc. It works but it is clumsy because only one should be true at a time.

like image 724
AlanObject Avatar asked Mar 04 '18 03:03

AlanObject


People also ask

What is the use of enum in angular?

Enums let you avoid using strings and having to remember what string values you used. They also allow you to lock down the values a property can be, so that a user can't just submit any value they want. So if you aren't using them, start using them, and if you are already using them, use them more, use them to a fault.

Can I use enum in template angular?

The TypeScript enum can be used directly in your class, but it has to be assigned to a local variable to be used in the template. The template can work only with your local variable and not the enum self. The template can access only objects exposed by the controller or the component.

Can I use enum as type in TypeScript?

Enums or enumerations are a new data type supported in TypeScript. Most object-oriented languages like Java and C# use enums. This is now available in TypeScript too. In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric or string values.

How does TypeScript enum work?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.


2 Answers

You can define the State enum outside of the class, possibly in another file:

export enum State {   init,    view,    edit,    create,    wait } 

and assign the enum to a public field in the class:

import { State } from "../models/app-enums.model";  @Component({   .... }) export class AbcComponent implements OnInit {   public StateEnum = State;   public state = State.init;   ... } 

That public field can then be used to refer to the enum in the template:

<div class="col" *ngIf="state === StateEnum.view"> ... </div> 
like image 55
ConnorsFan Avatar answered Sep 19 '22 22:09

ConnorsFan


You can define a method or getter and compare your current state value with the enum already imported. Like this:

import { State } from "../models/state-enum.ts";  @Component({   .... }) export class AbcComponent implements OnInit {   private state: State = State.init;   ...   get isView() {     return this.state === State.view;   } } 

template.html

<div *ngIf="isView">Oh is view!</div> 
like image 29
Daniel C. Avatar answered Sep 22 '22 22:09

Daniel C.