git clone https://github.com/angular-in-action/stocks-enhanced
cd stocks-enhanced
git checkout start
ng g d directives/card-type
import { Directive, ElementRef, Input, OnInit } from '@angular/core';
@Directive({
selector: '[cardType]'
})
export class CardTypeDirective implements OnInit {
@Input() cardType: number = 0;
@Input() increaseClass = 'increase';
@Input() decreaseClass = 'decrease';
constructor(private el: ElementRef) { }
ngOnInit() {
if (this.cardType) {
if (this.cardType >= 0) {
this.el.nativeElement.classList.add(this.increaseClass);
this.el.nativeElement.classList.remove(this.decreaseClass);
} else if (this.cardType <= 0) {
this.el.nativeElement.classList.add(this.decreaseClass);
this.el.nativeElement.classList.remove(this.increaseClass);
} else {
this.el.nativeElement.classList.remove(this.increaseClass);
this.el.nativeElement.classList.remove(this.decreaseClass);
}
}
}
}
<div class="mdl-card stock-card mdl-shadow--2dp" [cardType]="stock.change" [@fadeIn]="'in'" style="width: 100%;">