My directive, powerEntry, has different CSS classes that I want to add or remove based on the model state.
Currently, in my link function, I have some logic like this:
Script.JS
if (calcState.availablePoints > 0 && isHighEnoughLevel) {
levelUpBtnClass = 'enabled';
} else if (calcState.availablePoints === 0 ) { // TODO: If ability is at cap, also disabled.
levelUpBtnClass = 'disabled';
}
if (power.currentRank<=1) {
powerRankClass = 'standard';
} else {
powerRankClass = 'enhanced';
}
HTML
<img class="powerIcon" ng-src="/images/heroes/{{hero.name}}/powers/{{power.iconSrc}}">
<span class="powerRank" ng-class="powerRankClass">{{power.currentRank}}</span>
<div class="levelUpBtn" ng-class="levelUpBtnClass"></div>
While this setup works fine, I am unsure if it is the most optimal solution. It appears that ng-class can handle ternary operators, but including complex JavaScript expressions in my HTML doesn't feel right.
Is there a better approach for handling this? Are there any performance considerations between the different methods?