Add description attribute to enum and read this description in Angular

Create enum File

Create a file in the named like enums.enum.ts. Add in a Enum:

export enum Fruits{
    Apple= 1,
    Oranges = 2
}

Create the Constant File

Create a file in the named like constants.ts. Add in a Enum constant for description :

export const FruitsLabel = new Map([
  [Fruits.Apple, 'Yummy Apples'],
  [Fruits.Oranges , 'Big Oranges']
]);

Load the Constant in the Component

import {FruitsLabel} from './Constants';

ngOnInit(): void 
{
console.log(FruitsLabel.get(Fruits.Apple));
}

return // Yummy Apples

Leave a Reply

Your email address will not be published. Required fields are marked *