Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 convert Enum int to string

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?

like image 471
tony09uk Avatar asked Jan 12 '18 00:01

tony09uk


People also ask

How to convert enum value to string in TypeScript?

To convert a numeric enum to a string, use bracket notation to access a specific value on the enum to get its name, e.g. NumericEnum[NumericEnum. Yes] . Similarly, you can convert a string enum to string, by using dot notation to access a specific property. Copied!

How do you convert a string value to a specific enum type in C#?

TryParse() method converts the string representation of enum member name or numeric value to an equivalent enum object. The Enum. TryParse() method returns a boolean to indicate whether the specified string is converted to enum or not. Returns true if the conversion succeeded; otherwise, returns false .

What is angular enum?

Enums are one of the features exclusive to TypeScript. We can use them to define a list of named constants, which lets us create easier-to-understand code by documenting distinct cases. TypeScript includes numeric and string-based enums. And we can also assign computed values to non-const enums.


1 Answers

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>
like image 101
Dhanika Avatar answered Sep 22 '22 09:09

Dhanika