How to use a function and expression in a ngClass using Angular?

623 views Asked by At

I'm currently trying to show a list of Pokemons that have a different background color depending on their type. I would like to implement a feature where the border of the selected pokemon also shows with a gold border color. If I use them one at a time they work just fine but i'm having trouble using them together.

My html is the following:

<h1>Pokédex</h1>
<p [hidden]="!selectedPokemon">Geselecteerde pokemon: {{selectedPokemon}}</p>
<div class="wrapper">
  <app-pokemon (selectedPokemon)="highlightPokemon($event)"
               *ngFor="let item of pokemons"
               [pokemon]="item"
               [ngClass]="{getType(item.type), item.name === selectedPokemon ? 'select' : ''}">
  </app-pokemon>
</div>

My getType function is the following:

getType(pokemonType: string): string {
    pokemonType = pokemonType.toLowerCase();
    switch(pokemonType) {
      case 'grass': {
        return 'grass'
      }
      case 'fire': {
        return 'fire'
      }
      case 'water': {
        return 'water'
      }
      default: {
        return 'grass'
      }
    }
  }

The error my IDE is complaining about:

enter image description here

I also tried the following:

[ngClass]="getType(item.type), item.name === selectedPokemon ? 'select' : ''">

enter image description here

Thanks a lot for helping!

4

There are 4 answers

0
Đỗ văn Thắng On BEST ANSWER

You can using [className]="getType(item.type)" and [class.select]="item.name === selectedPokemon"

0
Danish Dullu On
<h1>Pokédex</h1>
<p [hidden]="!selectedPokemon">Geselecteerde pokemon: {{selectedPokemon}}</p>
<div class="wrapper">
<app-pokemon (selectedPokemon)="highlightPokemon($event)"
           *ngFor="let item of pokemons"
           [pokemon]="item"
class="{{getType(item.type)}}"
           [ngClass]="item.name === selectedPokemon ? 
'select' : ''">
</app-pokemon>
</div>

this is working code, what i did is selected class will come from ngclass while background class will come through getType function

2
AmiReza On

using functions in html templates is a bad practice, try to dont use them. but you can use like this for add multiple class conditionally by ngClass in angular

<div [ngClass]="[yourFunction(value), value === 2 && 'your-class']">123</div>
1
Ashutosh Goyal On

You can use expressions in the ngclass using the following syntax

home.html

<ion-item >
   <ion-input (ionFocus)="openJobOrderMeters($event)" formControlName="jobordermeter" [ngClass]="{'invalid-input': isInvalid('jobordermeter')}" placeholder="Job Order/ Account Number"
   value="{{ joborder }}" ></ion-input>
   <ion-icon name="arrow-dropright"></ion-icon>
</ion-item>

home.ts file

isInvalid(controlName: string): boolean {
  const control = this.smartmeterform.get(controlName);
  return control.invalid && control.touched;

}

Every time it calls the isInvalid function written in the home.ts file and acts according to the value returned by it. I have been marking the fields that are touched and it does not have valid input yet.