篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 08. Les Pipes相关的知识,希望对你有一定的参考价值。
import { Pipe, PipeTransform } from '@angular/core';
/*
* Affiche la couleur correspondant au type du pokémon.
* Prend en argument le type du pokémon.
* Exemple d'utilisation:
* {{ pokemon.type | pokemonTypeColor }}
*/
@Pipe({name: 'pokemonTypeColor'})
export class PokemonTypeColorPipe implements PipeTransform {
transform(type: string): string {
let color: string;
switch (type) {
case 'Feu':
color = 'red lighten-1';
break;
case 'Eau':
color = 'blue lighten-1';
break;
case 'Plante':
color = 'green lighten-1';
break;
case 'Insecte':
color = 'brown lighten-1';
break;
case 'Normal':
color = 'grey lighten-3';
break;
case 'Vol':
color = 'blue lighten-3';
break;
case 'Poison':
color = 'deep-purple accent-1';
break;
case 'Fée':
color = 'pink lighten-4';
break;
case 'Psy':
color = 'deep-purple darken-2';
break;
case 'Electrik':
color = 'lime accent-1';
break;
case 'Combat':
color = 'deep-orange';
break;
default:
color = 'grey';
break;
}
return 'chip ' + color;
}
}
<h1 class='center'>Pokémons</h1>
<div class='container'>
<div class="row">
<div *ngFor='let pokemon of pokemons' class="col s6 m4">
<div class="card horizontal" (click)="selectPokemon(pokemon)" pkmn-border-card>
<div class="card-image">
<img [src]="pokemon.picture">
</div>
<div class="card-stacked">
<div class="card-content">
<p>{{ pokemon.name }}</p>
<!-- on ajoute le pipe pour les dates, avec le bon format : -->
<p><small>{{ pokemon.created | date:"dd/MM/yyyy" }}</small></p>
<!-- on utilise la directive ngFor pour afficher tous les types
d'un pokémon donnée -->
<span *ngFor='let type of pokemon.types' class="{{ type | pokemonTypeColor }}">{{ type }}</span>
</div>
</div>
</div>
</div>
</div>
</div>