It's not obvious what you want here, but let me explain a few things ...
I created a blog post saying this:
How to use TypeScript Enumerations, especially with Angular 2+
But here I will include all the information:
An enumeration is just an object. Enum says something like this in JavaScript:
{ 0: "offline", 1: "available", 2: "busy", 3: "away", available: 1, busy: 2, away: 3, offline: 0 }
The advantage of input is very limited in transfers.
This line is valid:
var value = <AgentStatus>"offline";
But this is not useful because
value == AgentStatus.offline // <- false, because it "offline" == 0
So, you should always store your values ββas numbers, which you can get as follows:
How to convert a string to an enumeration
var value = AgentStatus["offline"]; // so value is now 0 // You can also use this, which only gives IDE hints, no runtime benefit var value: AgentStatus = AgentStatus["offline"];
This does the previous comparison job:
value == AgentStatus.offline // <- true, because it 0 == 0
Now a few questions remain:
How do you get the equivalent of a string?
AgentStatus.offline // 0 AgentStatus[AgentStatus.offline] // -> AgentStatus[0] -> "offline"
How can you get all the possible enumeration values?
var options : string[] = Object.keys(AgentStatus);
Gotcha
If you write this in your template:
{{AgentStatus[myValue]}}
It will not work because it does not have access to imported types (it will be executed later by Angular).
To make it work, your component will need to have a reference to the type / enumeration object, for example:
export class MyComponent {
Runnable demo
Here is an example that explains everything I pointed out here:
http://plnkr.co/edit/vOeeDrCI6CmsXdWaMtwG?p=preview
Look in the file: app/app.component.ts .