11

I would like to display my enum as a string but it display as a number.

I am receiving a json object from a web service and mapping it to my typescript object

getProperties(): Observable<Property[]> {
    return this.http.get<Property[]>(this.getallagentsproperties + '1');
}

export enum LetType {
    Notspecified = 0,
    LongTerm = 1,
    ShortTerm = 2,
    Commercial = 4
}

export class Property {
    ...other stuff...
    letType: LetType;
    ...other stuff...
}

My component makes the call and adds it to the relevant property

import { Component, OnInit } from '@angular/core';
import { Property } from './Property';
import { PropertyService } from '../properties.service';

@Component({
  selector: 'app-properties',
  templateUrl: './properties.component.html',
})

export class PropertiesComponent implements OnInit {
  properties: Property[];
  constructor(private propertyService: PropertyService) { }
  ngOnInit() {
    this.getProperties()
  }
  getProperties(): void {
    this.propertyService.getProperties()
        .subscribe(p => this.properties = p);
  }
}

When I display {{property.letType}} in my template is displays:

4 I want it to display Commercial

I have tried to follow the answer found here in my template I added

{{LetType[property.letType]}}

and in my Componant I added

LetType = this.LetType;

but I always get the below error in the console

Cannot read property '4' of undefined

What am I doing wrong?

1

3 Answers 3

26

You don't need to create a pipe here. Here is my answer.

let-type.enum.ts

export enum LetType{
  Type1 = 1,
  Type2 = 2,
  Type3 = 3,
  Type4 = 4
}

property.model.ts

export interface Property{
   ...other stuff...
   letType: LetType;
   ...other stuff...
}

properties.component.ts

import { Component, OnInit } from '@angular/core';
import { Property} from './property.model';
import { LetType} from './let-type.enum';
import { PropertyService } from '../properties.service';

@Component({
   selector: 'app-properties',
   templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {

  properties: Property[] = [];
  LetType = LetType;

  constructor(private propertyService: PropertyService) { }

  ngOnInit() {
     this.getProperties();
  }

  getProperties() {
    this.propertyService.getProperties().subscribe(result => {
         this.properties = result
    });
  }
}

Then in your html

<ng-container matColumnDef="columnName">
   <th mat-header-cell *matHeaderCellDef >Types</th>
   <td  mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>
</ng-container>
Sign up to request clarification or add additional context in comments.

Comments

9

Thanks to @Harry Ninh comment I was able to solve my issue.

{{property.letType}} displayed 0 I wanted it display My Enum Value

note: I did not ask for the split casing in my question but I have provided that here as I feel many people who are displaying an enum as a string value could find it useful

Step 1 Add required enum (LetType in my case) to your component

import { Component, OnInit, Type } from '@angular/core';
import { Property, LetType} from './Property';
import { PropertyService } from '../properties.service';

@Component({
    selector: 'app-properties',
    templateUrl: './properties.component.html',
})
export class PropertiesComponent implements OnInit {

    properties: Property[];
    LetType = LetType;

    constructor(private propertyService: PropertyService) { }

    ngOnInit() {
        this.getProperties();
    }

    getProperties(): void {
        this.propertyService.getProperties()
            .subscribe(p => this.properties = p);
    }
}

step 2 create custom pipe to convert your number to a string

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'eNumAsString'
})

export class ENumAsStringPipe implements PipeTransform {
    transform(value: number, enumType: any): any {
        return enumType[value].split(/(?=[A-Z])/).join().replace(",", " ");;
    }
}

Step 3 add your pipe to the table

...other html table tags...
    <td>{{property.letType | eNumAsString:LetType}}</td>
...other html table tags...

1 Comment

If I don't apply the pipe I get an empty combo box
9

You can do this without pipe:

LetType : typeof 
LetType = LetType ;

and in the html page:

<td  mat-cell *matCellDef="let element">{{LetType[element.letType]}}</td>

as before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.