0

I'm getting an array of objects with some properties from a .NET Controller using the following Typescript:

import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-fetch-data',
  templateUrl: './fetch-data.component.html'
})
export class FetchDataComponent {
  public celebreties: Celebrety[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<Celebrety[]>(baseUrl + 'celebrety').subscribe(result => {
      this.celebreties = result;
    }, error => console.error(error));
  }
}
export enum Gender {
  Male = 0,
  Female = 1,
  Unknown = 2,
}
interface Celebrety {
  name: string;
  birthDate: Date;
  gender: Gender;
  role: string;
  imageUrl: string;
}

The template html I have is this:

<h1 id="tableLabel">Celebereties</h1>

<p *ngIf="!celebreties"><em>Loading...</em></p>

<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="celebreties">
  <thead>
    <tr>
      <th>Name</th>
      <th>Birth Date</th>
      <th>Gender</th>
      <th>Role</th>
      <th>Image</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let celebrety of celebreties">
      <td>{{ celebrety.name }}</td>
      <td>{{ celebrety.birthDate }}</td>
      <td>{{ celebrety.gender }}</td>
      <td>{{ celebrety.role }}</td>
      <td><img src="{{ celebrety.imageUrl }}"/></td>
    </tr>
  </tbody>
</table>

This works fine but shows numbers for the gender. I'm Trying to display the names and not the numbers, but if I use:

<td>{{ Gender[celebrety.gender] }}</td>

it gives an undefined error. Why isn't the enum recognized inside the angular brackets?

Ronen Festinger
  • 2,260
  • 1
  • 23
  • 32

2 Answers2

1

I found a hack. I wrote a function that returns the value and I call it from the html:

<h1 id="tableLabel">Celebereties</h1>

<p *ngIf="!celebreties"><em>Loading...</em></p>

<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="celebreties">
  <thead>
    <tr>
      <th>Name</th>
      <th>Birth Date</th>
      <th>Gender</th>
      <th>Role</th>
      <th>Image</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let celebrety of celebreties">
      <td>{{ celebrety.name }}</td>
      <td>{{ celebrety.birthDate | date }}</td>
      <td>{{ GetGenderNameByValue(celebrety.gender) }}</td>
      <td>{{ celebrety.role }}</td>
      <td><img src="{{ celebrety.imageUrl }}" /></td>
      <td><button class="btn btn-primary" (click)="Delete()">Delete</button></td>
    </tr>
  </tbody>
</table>
<button class="btn btn-primary" (click)="Reset()">Reset</button>
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-fetch-data',
  templateUrl: './fetch-data.component.html'
})
export class FetchDataComponent {
  public celebreties: Celebrety[];

  public Reset() {
  }
  public Delete() {
  }
  public GetGenderNameByValue(value : number) {
    return Gender[value];
  }

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<Celebrety[]>(baseUrl + 'celebrety').subscribe(result => {
      this.celebreties = result;
    }, error => console.error(error));
  }
}
export enum Gender {
  Male,
  Female,
  Unknown,
}
interface Celebrety {
  name: string;
  birthDate: Date;
  gender: Gender;
  role: string;
  imageUrl: string;
}
Ronen Festinger
  • 2,260
  • 1
  • 23
  • 32
0

Enums were not really designed for this; however, you can do it like this:

Object.keys(Gender)[Object.keys(Gender).length / 2 + celebrety.gender]

enter image description here

The above code is assuming that celebrety.gender is a number (the value assigned from the enum) if it is actually the text, you would do:

Object.keys(Gender)[Object.keys(Gender).length / 2 + Gender[celebrety.gender]]

Which is obviously redundant since you already have the text "male"/"female"/"unknown"

It looks kind of hacky because enums are meant to be inserted into the code at compilation time, not be used as objects.

Daniel
  • 1,392
  • 1
  • 5
  • 16
  • I get ERROR TypeError: _co.Object is undefined View_FetchDataComponent_3 FetchDataComponent.html:19 Angular 29 RxJS 5 Angular 17 RxJS 16 FetchDataComponent fetch-data.component.ts:11 Angular 15 RxJS 44 – Ronen Festinger Mar 26 '22 at 20:42
  • Uhh I noticed an error and fixed it, try the updated code. – Daniel Mar 26 '22 at 20:43
  • tested it in deno and it definitely should work. (screen shot of it working included) tell me if you are still having problems – Daniel Mar 26 '22 at 20:48
  • The value I have on display is int, i want to display the name. I get the same error. Do you mean to put it like this? {{ Object.keys(Gender)[Object.keys(Gender).length / 2 + celebrety.gender] }} ? Why isn't it designed for it? are you saying I am not supposed to use enums for properties before serializing the objects and sending to the client? – Ronen Festinger Mar 26 '22 at 20:48
  • I used the default vs2019 ASP.NET template with angular and just changed the properties. – Ronen Festinger Mar 26 '22 at 20:51
  • Are you still not able to get the text to render? enums are just a way to assign aliases to values. There is no assumption that the alias would need to be fetched and reverse output. here is the ts documentation: https://www.typescriptlang.org/docs/handbook/enums.html – Daniel Mar 26 '22 at 20:55
  • The problem is not with the typescript, the problem is that in the Html, inside the curly brackets, The Enum is undefined. – Ronen Festinger Mar 26 '22 at 20:57
  • Oh, I think that is because you are not passing the enum Gender in your @Component – Daniel Mar 26 '22 at 21:02
  • ok, so how do i do that? – Ronen Festinger Mar 26 '22 at 21:04
  • https://stackoverflow.com/questions/52713588/display-enum-key-as-string-in-typescript-angular-4 – Daniel Mar 26 '22 at 21:15
  • It didn't help, but by trial and error I did a hack. I'm new to these languages. They really should allow recognition of the enum. I will post an answer. – Ronen Festinger Mar 26 '22 at 22:26