I am using Angular 7.
I got this HTML:
<section class="carddiv">
<section class="gameMessages">
<p [ngClass] ="{'messageMiddleOfGameNoPairs' : gameModeVar === GameMode.NONE_SELECTED || gameModeVar === GameMode.ONE_SELECTED,
'messageSuccess' : gameModeVar === GameMode.TWO_SELECTED_SUCCESS || gameModeVar === GameMode.GAME_OVER,
'messageFailure' : gameModeVar === GameMode.TWO_SELECTED_FAILURE}"> {{currentMoveMessage}} </p>
</section>
....
</section>
Inside the component's class:
enum GameMode
{
NONE_SELECTED = 0,
ONE_SELECTED = 1,
TWO_SELECTED_SUCCESS = 2,
TWO_SELECTED_FAILURE = 3,
GAME_OVER = 4
};
@Component({
selector: 'app-cardcomponent',
templateUrl: './cardcomponent.component.html',
styleUrls: ['./cardcomponent.component.css'],
})
export class CardcomponentComponent implements OnInit
{
currentMoveMessage: string;
gameModeVar : GameMode;
constructor(public cardService: CardService)
{
this.currentMoveMessage = "message";
this.gameModeVar = GameMode.NONE_SELECTED;
...
}
When I load the page, nothing is rendered because of the ngClass. If I use the numeric values of the enum, meaning in the HTML I'm changing the comparison of gameModeVar to gameModeVar === 1 instead of gameModeVar === GameMode.ONE_SELECTED (but for all values, not just 1) , it does work.
1) Is the way I'm declaring the enum inside the same file of the class above the @Component (it couldn't be declared after it without compilation error), the correct way to do it, as it's supposed to be private to this component?
2) What's the issue with the ngClass ?
GameMode = GameMode;stackblitzwith issue.