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.
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.
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.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With