Is there a method to create a conditional expression similar to ng-class
?
For instance, I attempted the following:
<span ng-class="{test: 'obj.value1 == \'someothervalue\''}">test</span>
The problem with this code is that regardless of the value of obj.value1
, the class test will always be applied. To illustrate:
<span ng-class="{test: obj.value2}">test</span>
If obj.value2
does not evaluate to a truthy value, the class will not be applied. In the first example, I can overcome this issue by using:
<span ng-class="{test: checkValue1()}">test</span>
Where the checkValue1
function is defined as follows:
$scope.checkValue1 = function() {
return $scope.obj.value === 'somevalue';
}
I am curious if this is the intended behavior of ng-class
. I am also in the process of creating a custom directive where I intend to achieve something similar. However, I am facing challenges in monitoring an expression (which might be impossible and could explain why it functions in this manner).
Here is a plnkr demonstrating my query.