Pipes utilization to display friendly labels for Enums in Angular

In this article, I will show a simple solution to display a user-friendly labels for enumerations in angular using Pipes. The solution is an example of reuse-based software engineering strategy

It define a design pattern (template) that can be repeatedly applied to a commonly recurring situation in software design. The patterns ensure a good practice environment, quality assurance, and compatible test results. The development process involves implementing the design pattern in a reusable way, allowing for easier maintenance and updates in the future. This approach promotes efficiency, reduces redundancy, and enhances the overall software development lifecycle.

For this purpose an common class EnumViewModel width following definition is used.

export class EnumViewModel {
    id: number;
    name: string;

    constructor(){
        this.id = -1;
        this.name = 'Default';
    }
}

An example enumeration:

export enum AircraftStatus {
    New = 1,
    Operational = 2,
    Maintenance = 3,
    ForLeaseOrSale = 4,
    Retired = 5
}

For each enumeration an option array of type EnumViewModel is defined

export const AircraftStatusOpt: EnumViewModel[] = [
    {
        id: 1,
        name: 'New'
    },
    {
        id: 2,
        name: 'Operational'
    },
    {
        id: 3,
        name: 'Maintenance'
    },
    {
        id: 4,
        name: 'For Lease Or Sale'
    },
    {
        id: 5,
        name: 'Retired'
    },
];

Next is a pipe definition:

@Pipe({ name: 'acstatus' })
export class AcStatusDisplayPipe implements PipeTransform {
    transform(value: number, args?: string[]): string {
        let rv: string = AircraftStatus[value];
        const data: EnumViewModel = AircraftStatusOpt.find(x => x.id === value);
        if (data) {
            rv = data.name;
        }
        return rv;
    }
}

Entire code of the solution can be found in this stackbiz project.

This is an screenshot of my stackblitz example where you can see utilization of this structural design-pattern in a table, select box and text. Described in this post pattern falls into Structural Design Pattern category.

Structural Design Pattern

A design pattern is a description or template that can be repeatedly applied to a commonly recurring problem in software design. There are three pattern types which are creational, structural, and behavioral patterns. Described in this post pattern falls into Structural category, that focuses more on how classes and objects are composed using the different structural techniques, and to form structures with more or altered flexibility.

Thank you.

  1. Hi there would you mind letting me know which hosting company you’re utilizing?
    I’ve loaded your blog in 3 different browsers and I must say
    this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a fair
    price? Many thanks, I appreciate it!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>