Make a function that returns the class you need if some boolean is true:
returnClass = true;
getClass() {
if(this.returnClass) {
return "myView";
} else {
return "";
}
}
and change your view:
<img *ngIf="returnClass" [ngClass]="getClass()" src="..">
Now if returnClass
is true, you know that your img
will have the desired class, so you can pass returnClass
into *ngIf
You could also remove the class by: this.returnClass = false
which would also hide the element.
This would become tedious with many classes, but will be reasonable for a few.