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?
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!
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 .
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With