Reputation: 1091
I have a string based enum. Without changing the enum class, I need to display different strings on my modal. My obj contains the enum. My code is as follows:
ENUM:
export enum FooEnum {
ONE,
TWO,
THREE
}
HTML:
<select class="form-control"
type="text"
id="foo"
[(ngModel)]="obj.foo"
required
name="foo"
#foo="ngModel">
<option *ngFor="let foo of fooToList()" [ngValue]="foo">{{foo}}</option>
</select>
TypeScript:
fooToList(): Array<string> {
const keys = Object.keys(FooEnum);
return keys.slice(keys.length / 2);
}
I would like to view ONE as Un, TWO as Deux etc. Could you help me pretty please ?
Note: I included angular 2 also because 2 and 4 are highly similar.
Upvotes: 6
Views: 21554
Reputation: 20037
You can initialise enums with values too such as
enum FooEnum {
ONE = 'Un',
TWO = 'Deux',
... etc
}
See documentation.
Note: This was introduced in TypeScript 2.4
Upvotes: 10