Angular 6 - Comparison of values

0

In the database I saved an integer to represent the user type. The integer can receive 1, 2, 3, 4, 5, 6 or 7.

When I show the type of user on the screen, I would have to do several * ngIf to show the value in the html component.

<td *ngIf="user.profile == 1"> Administrador </td>
<td *ngIf="user.profile == 2"> Avaliador </td>
<td *ngIf="user.profile == 3"> Responsável </td>

E outros 4 *ngIf para validar o restante...

So as not to need to do these * ngIf, would it have a better way? I did not want to put 7 validations just to show a result in the html component.

    
asked by anonymous 29.07.2018 / 00:15

1 answer

0

If you are going to display only one at a time, it may make more sense to "calculate" what will be displayed in the code and display with a function:

TS:

function nomePerfil(perfil: number) : string {
  switch (perfil) {
    case 1: return "Administrador ";        
    case 2: return "Avaliador";    
    ...     
    default: return "";
  }
}

HTML:

<td>{{nomePerfil(user.profile)}}</td>

EDITED

Using Enum , suggested by @EduardoVargas, the function is much simpler, just declare Enum :

TS

export enum Perfil {
  Administrador,
  Avaliador,
  Responsável,
}

HTML

<td>{{Perfil[user.profile]}}</td>

I believe that calling Enum as above works, if it does not work you can put this code inside the nomePerfil function suggested above.

    
30.07.2018 / 16:15