I can't get ngClass working in my Angular 5 app, the class is always showing regardless of if the boolean variable that I am using to show it conditionally is true or false.
I have a component that shows rating stars:
export class RatingStarComponent {
@Input() max: number;
@Input() initial: number;
@Input() readOnly: boolean;
@Output() onRating = new EventEmitter<Number>();
maxItem: any[];
ratedCount: number;
hideHover: boolean;
constructor() {}
ngOnInit() {
this.ratedCount = this.initial;
this.hideHover = this.readOnly;
this.maxItem = [];
for (var i = 0; i < this.max; i++) {
this.maxItem.push(i + 1);
}
}
toggleRating(s: number) {
this.ratedCount = s;
this.onRating.emit(this.ratedCount);
}
}
This is my html
<div [ngClass]="{ 'hideme': this.hideHover }">
<span class="icon" *ngFor="let s of maxItem">
<i [ngClass]="s <= this.ratedCount ? 'filled' : ''" class="fa fa-star" aria-hidden="true" (click)="toggleRating(s)"></i>
</span>
</div>
If I add {{this.hideHover}} to this html then it is reflecting properly, showing true or false depending on how I call it. I call the component like so:
<rating-star max=5 readOnly="true" initial=4></rating-star>
However my hideme class is always being added to the component. What am I doing wrong.
<rating-star max=5 [readOnly]="false" initial=4></rating-star>
instead of<rating-star max=5 readOnly="false" initial=4></rating-star>
– Harry Ninh