The voteClass
is not changing after the first call, although the return for getVoteClass
is, but it does not get called again.
To not change your example, the right way to go would be:
function checkForVoteClass() {
return getVoteClass(scope.block.id);
}
scope.$watch(checkForVoteClass,function(newValue, oldValue) {
element.addClass(newValue);
})
But have in mind that everytime an $apply occur anywhere in the system, this is going to get reevaluated.
So, when getVoteClass would return a different value? On user interaction? If it is, then the best way to go should be something like this:
<span ng-click="vote='bad'">Bad</span>
<span ng-click="vote='good'">Good</span>
<span id="thisIsYourElement"
ng-class="{bad: 'badCssClass', good: 'goodCssClass', 'undefined': 'noVoteClass'}[vote]"
>Your content</span>
Of course, its pretty simplified.